0.16.4 • Published 5 years ago

reactjs-input-validator v0.16.4

Weekly downloads
89
License
MIT
Repository
github
Last release
5 years ago

ReactJS Input Validator

Input validator for React with the awesomeness of validator.js

This module saves you time in three ways.

  • Clean code. You don't have to write custom validations for every input field in your form.
  • No learning curve. You don't have to deal with any new syntax. Pass the validations and error messages you want as props to our component and that's it!
  • Inbuilt styles & error messages. We ship you custom styles and error messages for the validation of your input fields along with form validation.

Usage

Basic Usage

<Field
    validator="isEmail" required
    label="Email" name="email" placeholder="Email"
/>

Email validation

Install

yarn add reactjs-input-validator
npm install reactjs-input-validator --save

Import Library

To use reactjs-input-validator in your react app, you should import it first.

// ES6
import { Field } from 'reactjs-inpupt-validator';
// or in ES5
var Field = require('reactjs-input-validator');

Import CSS

Finally, you need to link bootstrap to your application.

// index.html
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

Props

NameTypeDefaultDescription
classNamestringform-controlBase CSS class for the component. Generally one should only change className to provide new, non-Bootstrap, CSS styles for a component.
idstringSets id for the input field.labelstringCalls element behind the scenes to set Label for the input field.
lengthnumberValidate if length of the input value matches the value passed. (Default error message available if this validation fails)
lengthErrMsgstringCustom error message if length validation fails.
maxLengthnumberValidate if length of the input value is not greater than maximum characters length allowed. (Default error message available if this validation fails)
maxLengthErrMsgstringCustom error message if maxLength validation fails.
minLengthnumberValidate if length of the input value is not lesser than minimum characters length allowed. (Default error message available if this validation fails)
minLengthErrMsgstringCustom error message if minLength validation fails.
namestringUsed to reference input elements and to reference it's data.
onChangefuncPass this prop to every Input component to get form data and form validation.
placeholderstringPass placeholder for your Input component.
requiredbooleanfalseUse this prop to make your input field required. (Default error message available if this validation fails)
requiredErrMsgstringCustom error message if required validation fails.
setReffunctionSet reference to your Input
typestringtextThe type attribute specifies the type of element to display. Supported types: [email, password, text, color, date, datetime-local, month, number, range, hidden, search, tel, url, week] Not supported types: [button, checkbox, file, image, radio, reset, submit, time]
validatorstringOne of the validators from validator.js ["contains", "equals", "isAfter", "isAlpha", "isAlphanumeric", "isAscii", "isBase64", "isBefore", "isBoolean", "isByteLength", "isCreditCard", "isCurrency", "isDataURI", "isDecimal", "isDivisibleBy", "isEmail", "isEmpty", "isFQDN", "isFloat", "isFullWidth", "isHalfWidth", "isHash", "isHexColor", "isHexadecimal", "isIP", "isISBN", "isISIN", "isISO31661Alpha2", "isISO8601", "isISRC", "isISSN", "isIn", "isInt", "isJSON", "isLatLong", "isLength", "isLowercase", "isMACAddress", "isMD5", "isMimeType", "isMobilePhone", "isMongoId", "isMultibyte", "isNumeric", "isPort", "isPostalCode", "isSurrogatePair", "isURL", "isUUID", "isUppercase", "isVariableWidth", "isWhitelisted", "matches"]
validatorErrMsgstringCustom error message if validator validation fails.

Sign up form Demo

https://srikanthbandaru.github.io/reactjs-input-validator/

import React, { Component } from 'react';
import { Row, Col } from 'react-bootstrap';
import { Field, formInputData, formValidation } from 'reactjs-input-validator';


export default class App extends Component {
  constructor() {
    super();
    this.state = {
      data: {},
    };
    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  handleChange(event, inputValue, inputName, validationState, isRequired) {
    const value = (event && event.target.value) || inputValue;
    const { data } = this.state;
    data[inputName] = { value, validation: validationState, isRequired };
    this.setState({
      data,
    });
    // if you want access to your form data
    const formData = formInputData(this.state.data); // eslint-disable-line no-unused-vars
    // tells you if the entire form validation is true or false
    const isFormValid = formValidation(this.state.data); // eslint-disable-line no-unused-vars
  }

  handleSubmit(event) {
    event.preventDefault();
    const isFormValid = formValidation(this.state.data);

    if (isFormValid) {
      // do anything including ajax calls
      this.setState({ callAPI: true });
    } else {
      this.setState({ callAPI: true, shouldValidateInputs: !isFormValid });
    }
  }

  render() {
    const passwordValue = this.state.data.password && this.state.data.password.value;

    return (
      <form className="example">
        <Row>
          <Col md={6}>

            {/*
              Input required validation check with
              library default error messages
            */}
            <Field
              required
              label="Full Name" name="fullName" placeholder="First Last"
              onChange={this.handleChange}
              value={this.state.data.fullName}
              shouldValidateInputs={this.state.shouldValidateInputs}
            />
          </Col>
          <Col md={6}>

            {/*
              Input required validation check with
              isEmail validation and
              library default error messages
            */}
            <Field
              validator="isEmail" required
              label="Email" name="email" placeholder="Email"
              onChange={this.handleChange}
              value={this.state.data.email}
              shouldValidateInputs={this.state.shouldValidateInputs}
            />

          </Col>
        </Row>
        <Row>
          <Col md={6}>

            {/*
              Input required validation check with
              isAlphanumeric validation
              and minimum character length validation with
              custom error msg only when minLength validation fail
            */}
            <Field
              validator="isAlphanumeric" required minLength={8}
              minLengthErrMsg="Short passwords are easy to guess. Try one with atleast 8 characters"
              label="Create a password" name="password" type="password" placeholder="Password"
              onChange={this.handleChange}
              value={this.state.data.password}
              shouldValidateInputs={this.state.shouldValidateInputs}
            />

          </Col>
          <Col md={6}>

            {/*
              Input required validation check with
              equals validation with
              custom error msg only when equals validation fail
            */}
            <Field
              validator="equals" required comparison={passwordValue}
              validatorErrMsg="These passwords don't match. Try again?"
              label="Confirm password" name="confirmPassword" type="password" placeholder="Password"
              onChange={this.handleChange}
              value={this.state.data.confirmPassword}
              shouldValidateInputs={this.state.shouldValidateInputs}
            />

          </Col>
        </Row>

        {/*
          Input required validation check with
          custom error msg only when required validation fail
        */}
        <Field
          required
          requiredErrMsg="Enter your address so we can send you awesome stuff"
          label="Address" name="address" placeholder="1234 Main St"
          onChange={this.handleChange}
          value={this.state.data.address}
          shouldValidateInputs={this.state.shouldValidateInputs}
        />

        {/*
          No validation
        */}
        <Field
          label="Address 2"
          name="address2" placeholder="Apartment, studio, or floor"
          onChange={this.handleChange}
          value={this.state.data.address2}
          shouldValidateInputs={this.state.shouldValidateInputs}
        />

        <Row>
          <Col md={6}>

            {/*
              Input required validation check with
              maximum character length validation with
              library default error messages
            */}
            <Field
              maxLength={20} required label="City"
              name="inputCity"
              onChange={this.handleChange}
              value={this.state.data.inputCity}
              shouldValidateInputs={this.state.shouldValidateInputs}
            />

          </Col>
          <Col md={3}>

            {/*
              You can declare other input types too
            */}
            <label htmlFor="inputState">State</label>
            <select
              name="inputState" className="form-control"
              onChange={this.handleChange}
              value={this.state.data.inputState ? this.state.data.inputState.value : ''}
            >
              <option>Choose...</option>
              <option value="ALABAMA">ALABAMA</option>
              <option value="ALASKA">ALASKA</option>
              <option value="ARIZONA">ARIZONA</option>
              <option>...</option>
            </select>

          </Col>
          <Col md={3}>

            {/*
              Input required validation check with
              maximum character length validation with
              library default error messages
            */}
            <Field
              validator="isPostalCode" locale="US" required maxLength={10}
              validatorErrMsg="Enter a valid US Zip"
              label="Zip" name="inputZip"
              onChange={this.handleChange}
              value={this.state.data.inputZip}
              shouldValidateInputs={this.state.shouldValidateInputs}
            />
          </Col>
        </Row>
        <button
          type="submit" onClick={this.handleSubmit} className="btn btn-primary"
        >Sign up
        </button>
        {this.state.callAPI
          ?
            <pre className="resultBlock">
              {JSON.stringify(formInputData(this.state.data), null, 4)}
            </pre>
          : null
        }
      </form>
    );
  }
}

License

MIT. Copyright 2018 (c) Srikanth Bandaru.

0.16.4

5 years ago

0.16.3

5 years ago

0.16.2

5 years ago

0.16.1

5 years ago

0.16.0-beta.1

6 years ago

0.16.0-beta.0

6 years ago

0.15.8

6 years ago

0.15.7

6 years ago

0.15.6

6 years ago

0.15.5

6 years ago

0.15.4

6 years ago

0.15.3

6 years ago

0.15.2

6 years ago

0.15.1

6 years ago

0.15.0

6 years ago

0.14.7

6 years ago

0.14.6

6 years ago

0.14.5

6 years ago

0.14.4

6 years ago

0.14.3

6 years ago

0.14.2

6 years ago

0.14.1

6 years ago

0.14.0

6 years ago

0.13.11

6 years ago

0.13.10

6 years ago

0.13.9

6 years ago

0.13.8

6 years ago

0.13.7

6 years ago

0.13.6

6 years ago

0.13.5

6 years ago

0.13.4

6 years ago

0.13.3

6 years ago

0.13.2

6 years ago

0.13.1

6 years ago

0.13.0

6 years ago

0.12.0

6 years ago

0.11.1

6 years ago

0.11.0

6 years ago

0.10.1

6 years ago

0.10.0

6 years ago

0.9.0

6 years ago

0.8.0

6 years ago

0.7.1

6 years ago

0.6.1

6 years ago

0.6.0

6 years ago

0.5.1

6 years ago

0.4.0

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago