1.0.2 • Published 4 years ago

@teragon/react-form-validator v1.0.2

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

@teragon/react-form-validator

Form Validation library

NPM JavaScript Style Guide Publish

Install

NPM

npm install --save @teragon/react-form-validator

Yarn

yarn add @teragon/react-form-validator

Usage

import React from 'react'

import { useValidatedState } from '@teragon/react-form-validator'

const NewUserForm = () => {
    const [ username, usernameErrors, setUsername ] = useValidatedState("", "string|required");

    return (
        <div>
            <input type="text" value={username} onChange={event => setUsername(event.target.value)} />
            { usernameErrors.length > 0 &&
                usernameErrors.map((error, index) => <p key={index}>{error}</p>)
            }
        </div>
    )
}

Validators

NameDescription
acceptedValidate if the value is true
afterValidates if a date is after the passed in date
after_or_equalValidates if a date is on or after the passed in date
alphaValidates that the value is alpha only
alpha_spaceValidates that the values is alpha with spaces only
alpha_numValidates that the value is alpha numeric only
alpha_num_spaceValidates that the value is alpha numeric with spaces only
alpha_num_dashValidates that the value is alpha numeric with dashes only
alpha_num_dash_spaceValidates that the value is alpha numeric with dashes or spaces
arrayValidates that the value is an array
beforeValidates that the value is a date before the passed in date
before_or_equalValidates that the value is a date on or before the passed in date
betweenValidates that the value is between two numbers
booleanValidates that the value is a boolean
card_expValidates that the value is a credit card expiration date
card_numValidates that the value is a credit card number
currencyValidates that the value is currency
dateValidates that the value is a date
date_equalsValidates that the value is a date that equals the passed in date
emailValidates that the value is an email address
inValidates that the value exists in the list of values passed in
integerValidates that the value is an integer
maxValidates that the value is smaller than the passed in value
minValidates that the value is larger than the passed in value
not_inValidates that the value is not in the list of passed in values
not_regexValidates that the value does not match the passed in regex
numericValidates that the value is numeric
phoneValidates that the value is a phone number
regexValidates that the value matches the passed in regex
requiredValidates that the value is not blank
sizeValidates that the value's size matches the passed in value
stringValidates that the value is a string
typeofValidates that the value matches the passed in type
urlValidates that the value is a URL

License

MIT © Craig Salajan