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

react-form-validators

v1.1.1

Published

form validators for react applications

Downloads

10

Readme

react-form-validators

Lightweight form validation library and methodology for ReactJS
To Install
  npm install --save react-form-validators

www.npm.com/package/react-form-validators

Table of Contents

  1. Validation Methods
  2. Configurations
  3. React Validation Approach

Validation Methods

  • 1.1 The minimumLen method takes input as the first argument. It also takes a config object with a required min property as the second argument.
    const configObj = {
      min: num
    };
    minimumLen(userInput [, configObj])
    // if input is shorter than min, will return 'is too short' by default
  • 1.2 The containsWhiteSpace method takes input as the first argument and an optional config object as a second argument.
    containsWhiteSpace(userInput [, configObj])
    // if input has white space, will return 'must not contain white space' by default
  • 1.3 The containsTrailingSpace method takes input as the first argument and an optional config object as a second argument.
     containsTrailingSpace(userInput [, configObj])
     // only returns 'must not begin or end with a space' by default, if input begins or end with a space
  • 1.4 The isRequired method takes input as the first argument and an optional config object as a second argument.
     isRequired(userInput [, configObj])
     // if input is empty after initial typing, returns 'is required' by default
  • 1.5 The isNotANumber method takes input as the first argument and an optional config object as a second argument.
     isNotANumber(userInput, [, configObj])
     // if input is NaN, returns 'is not a number' by default
  • 1.6 The isNotInRange method takes input as the first argument. It also takes a config object with required min & max properties as the second argument.
    const configObj = {
      min: num,
      max: num
    };
     isNotInRange(userInput [, configObj])
     // if input < min || input > max, will return 'is not in range' by default
  • 1.7 The alreadyExists method takes input as the first argument. It takes two additional required args:
    • Data array: two-dimensional array, array of objects, or array of strings
    • Config object with a required inputLabelName property, which specifies the property you want to test for existence.
    const configObj = {
      inputLabelName: 'String' (e.g. 'username')
    };
    alreadyExists(userInput [data, configObj])
    // if inputLabelName exists in the provided data structure, will return 'already exists' by default
  • 1.8 The validateEmail method takes input as the first argument. It also takes a config object with an optional caseSensitive property as the second argument.
    const configObj = {
      caseSensitive: boolean
    }
    validateEmail(email [, configObj])
    // if email format is invalid, will return 'is invalid e-mail format' by default
  • 1.9 The validateUSPhoneNumber method takes input as the first argument and an optional config object as a second argument.
    validateUSPhoneNumber(number [, configObj])
    // if number is not a valid U.S. phone number, will return 'is invalid U.S. phone number' by default

    /*
      VALID FORMATS:
    * 2125555555
    * (212)-555-5555
    * (212) 555-5555
    * 212-555-5555
    * +1-212-555-5555
    * +1-212-555-5555 ext 77
    * */
  • ... And there's more to come!

Back to top

Configurations

  • Every method can handle a config object with the errorMessage (String) property for custom messages.

Back to top

React Validation Approach

  • Install and import react-form-validators
import { Component } from 'react';
/* import validator methods */
import { containsWhiteSpace, isNotInRange } from 'react-form-validators';
/* make them iterable */
const methods = [containsWhiteSpace, isNotInRange];

class InputField extends Component {
  constructor(props) {
    super();
    this.state = {
      value: ''
    }
  }
  /* Example: capture input value in component state */
  handleChange(event) {
    this.setState({
      value: event.target.value
    });
  }

  render() {
    let { value } = this.state,
        errorMsg,
        errorNode,
        config = {};
    /* Loop over methods, save its return value to errorMsg variable */
    methods.some(method => {
      if (method === isNotInRange) {
        config = {
          min: 3,
          max: 7
        }
        errorMsg = method(value, config);
        return errorMsg;
      }
      else {
        errorMsg = method(value);
        return errorMsg;
      }
    });
    /* if the method returns a message, render it to a 'p' tag */
    if (errorMsg) {
      errorNode = <p>{ errorMsg }</p>
    }

    /* the errorNode below will render only if the method returns a message */
    return (
      <div>
        <input onChange={ this.handleChange } value={ this.state } />
        { errorNode }
      </div>
    );
  }
}
export default InputField;

Back to top