npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

simple-string-validator

v1.0.3

Published

A simple library to validate different inputs and strings

Downloads

15

Readme

Easy input validation with form validator

This library will help you validate any strings (emails, passwords, URLs, etc) and show an error if the validation fails.

Features:

  • Small size.
  • No extra npm dependecies.
  • Typescript support.
  • Full test coverage.
  • Support all platforms.

Table of contents

Quick start

For start using:

npm i simple-string-validator

or

yarn add simple-string-validator

Usage example

React:

import React, { useState } from 'react';
import validate from 'simple-string-validator';

function InputValidation() {
  const [email, setEmail] = useState('');
  const [emailErrors, setEmailErrors] = useState<string[] | null>();

  const submitForm = (e: React.ChangeEvent<HTMLFormElement>) => {
    e.preventDefault();
    setEmailErrors(null);
    const { errors } = validate(email, { isEmail: true, required: true });
    if (errors) {
      setEmailErrors(errors);
      return;
    }
    //submit form logic
  };

  return (
    <form onSubmit={submitForm}>
      <h1>Enter your email to subscribe.</h1>
      <input value={email} onChange={(e) => setEmail(e.target.value)} />
      {emailErrors &&
        emailErrors.map((error) => {
          return <p>{error}</p>;
        })}
      <button>Submit</button>
    </form>
  );
}

export default InputValidation;

You could get all the validation errors from the "errors" array.

You could also provide your custom error message (see accepted parameters below).

Settings

| Parameter | Type | Description | | ------------------------ | --------------------------------- | -------------------------------------------------------------------------------------------------------------------------- | | maxLength | number | Sets the maximum allowed string length. | | minLength | number | Sets the minimum allowed string length. | | required | boolean | Indicates whether this string is required | | numbersOnly | boolean | A flag that indicates that only numbers should be contained in the string. | | lettersOnly | boolean | A flag that indicates that only letters should be contained in the string. | | lettersAndNumbersOnly | boolean | A flag that indicates that only numbers and letters should be contained in the string. Special characters are not allowed. | | case | "lower", "upper", "both required" | Sets the case in which the string should be | | containSpecialCharacters | boolean | Indicates that the string must contain special characters. | | isEmail | boolean | Checks if a string is an email | | isURL | boolean | Checks if a string is an URL | | errorMessage | string | Allows you to set a custom error message that will be displayed if the string fails validation. |

Custom error messages example:

const { errors } = validate(email, {
      isEmail: true,
      required: true,
      errorMessage: 'Please, provide correct email'
    })

Typescript

This library has full Typescript support.

Bug reporting

You could send it to [email protected] or open an issue on https://github.com/ArtemChekhovskoi/form-validate/issues.

License

ISC Licensed. Artem Chekhvskoi, 2023.

Keywords

validate form inputs string validation validator password email