1.0.3 • Published 8 months ago

@aqpdev/react-form-validation v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

react-form-validation

React hook to validate form values inspired in CodeIgniter (PHP framework).

Installation

npm i @aqpdev/react-form-validation

Usage

import React from "react"
import {useFormValidation} from "@aqpdev/react-form-validation";

export default function Home() {
    const {handleForm, hasErrors, showError} = useFormValidation([
        {
            field_name: "username",
            rules: "required|min_length[5]|alpha_dash",
            errors: {
                required: "Username is required.",
                min_length: "Username must contains at least 5 characters.",
                alpha_dash: "Username may only contain alpha-numeric characters, underscores, and dashes."
            }
        },
        {
            field_name: "password",
            rules: "required|min_length[6]|alpha_with_upper_numeric_special",
            errors: {
                required: "Password is required.",
                min_length: "Password must contains at least 6 characters.",
                alpha_with_upper_numeric_special: "Password must contains letters (lowercase and uppercase), numbers and special characters.",
            }
        },
        {
            field_name: "confirm_password",
            rules: "required|matches[password]",
            errors: {
                required: "Confirm your password.",
                matches: "Passwords do not match.."
            }
        },
        {
            field_name: "email",
            rules: "required|valid_email",
            errors: {
                required: "Enter your email address.",
                valid_email: "Email address is not valid."
            }
        }
    ]);

    const myHandle = (data) => {
        //Data returns a JSON of form elements
        console.log(data);
    }

    return (
        <form onSubmit={(e) => handleForm(e, myHandle)}>
            <div className="mb-3">
                <label className="form-label">Username</label>
                <input type="text" name="username" className="form-control"/>
                {hasErrors('username') && <div className="text-danger">{showError('username')}</div>}
            </div>

            <div className="mb-3">
                <label className="form-label">Password</label>
                <input type="password" name="password" className="form-control"/>
                {hasErrors('password') && <div className="text-danger">{showError('password')}</div>}
            </div>

            <div className="mb-3">
                <label className="form-label">Confirm Password</label>
                <input type="password" name="confirm_password" className="form-control"/>
                {hasErrors('confirm_password') && <div className="text-danger">{showError('confirm_password')}</div>}
            </div>

            <div className="mb-3">
                <label className="form-label">Email</label>
                <input type="text" name="email" className="form-control"/>
                {hasErrors('email') && <div className="text-danger">{showError('email')}</div>}
            </div>

            <div>
                <button className="btn btn-primary">Submit</button><br/>
                <button type="button" className="btn btn-primary">Button blocked</button><br/>
            </div>
        </form>
    )
}

Rules Reference

RuleParameterDescriptionExample
requiredNoReturns error if the form element is empty.
matchesYesReturns error if the form element does not match the one in the parameter.matchesform_item
regex_matchYesReturns error is the form element does not match the regular expresion.regex_match/regex/
differsYesReturns error if the form element does not differ from the one in the parameter.differsform_item
min_lengthYesReturns error if the form element is shorter than the parameter value.min_length3
max_lengthYesReturns error if the form element is longer than the parameter value.max_length12
exact_lengthYesReturns error if the form element is not exactly the parameter value.exact_length8
greater_thanYesReturns error if the form element is less than or equal to the parameter value or not numeric.greater_than8
greater_than_equal_toYesReturns error if the form element is less than the parameter value, or not numeric.greater_than_equal_to8
less_thanYesReturns error if the form element is greater than or equal to the parameter value or not numeric.less_than8
less_than_equal_toYesReturns error if the form element is greater than the parameter value, or not numeric.less_than_equal_to8
in_listYesReturns error if the form element is not within a predetermined list.in_listred,blue,green
alphaNoReturns error if the form element contains anything other than alphabetical characters.
alpha_numericNoReturns error if the form element contains anything other than alpha-numeric characters.
alpha_numeric_spacesNoReturns error if the form element contains anything other than alpha-numeric characters or spaces. Should be used after trim to avoid spaces at the beginning or end.
alpha_dashNoReturns error if the form element contains anything other than alpha-numeric characters, underscores or dashes.
alpha_and_numericNoReturns error if the form element does not contain letters and numbers.
alpha_with_upper_and_numericNoReturns error if the form element does not contain letters (lowercase and uppercase) and numbers.
alpha_with_upper_numeric_specialNoReturns error if the form element does not contain letters (lowercase and uppercase), numbers and special characters.
numericNoReturns error if the form element contains anything other than numeric characters.
integerNoReturns error if the form element contains anything other than an integer.
decimalNoReturns error if the form element contains anything other than a decimal number.
is_naturalNoReturns error if the form element contains anything other than a natural number: 0, 1, 2, 3, etc.
is_natural_no_zeroNoReturns error if the form element contains anything other than a natural number, but not zero: 1, 2, 3, etc.
valid_urlNoReturns error if the form element does not contain a valid URL.
valid_emailNoReturns error if the form element does not contain a valid email address.
valid_ipNoReturns error if the form element does not contain a valid IP address (‘ipv4’ or ‘ipv6’).
if_requiresYesReturns error if the form element is filled and the other form element (parameter) is empty.if_requiresform_item
if_use_requiresYesReturns error if the form element is equal to first parameter and the other form element (second parameter) is empty.if_requiresuser,form_item
1.0.3

8 months ago

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago