All Projects → NewOldMax → react-native-validator-form

NewOldMax / react-native-validator-form

Licence: MIT license
Simple validator for react-native forms.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to react-native-validator-form

Ratifier
Ratifier is a form validation library for Android.
Stars: ✭ 123 (+485.71%)
Mutual labels:  validation, form
Neoform
✅ React form state management and validation
Stars: ✭ 162 (+671.43%)
Mutual labels:  validation, form
Fonk
Form schema validation library
Stars: ✭ 125 (+495.24%)
Mutual labels:  validation, form
Bootstrap Validate
A simple Form Validation Library for Bootstrap 3 and Bootstrap 4 not depending on jQuery.
Stars: ✭ 112 (+433.33%)
Mutual labels:  validation, form
node-input-validator
Validation library for node.js
Stars: ✭ 74 (+252.38%)
Mutual labels:  validation, form
React Form With Constraints
Simple form validation for React
Stars: ✭ 117 (+457.14%)
Mutual labels:  validation, form
Formhelper
ASP.NET Core - Transform server-side validations to client-side without writing any javascript code. (Compatible with Fluent Validation)
Stars: ✭ 155 (+638.1%)
Mutual labels:  validation, form
Vue Rawmodel
RawModel.js plugin for Vue.js v2. Form validation has never been easier!
Stars: ✭ 79 (+276.19%)
Mutual labels:  validation, form
React Advanced Form
Functional reactive forms. Multi-layer validation, custom styling, field grouping, reactive props, and much more.
Stars: ✭ 186 (+785.71%)
Mutual labels:  validation, form
Qmbform
Create simple Android forms
Stars: ✭ 184 (+776.19%)
Mutual labels:  validation, form
Ngx Dynamic Form Builder
FormBuilder + class-transformer + class-validator = dynamic form group builder for Angular10+
Stars: ✭ 93 (+342.86%)
Mutual labels:  validation, form
Pristine
Vanilla javascript form validation micro-library
Stars: ✭ 197 (+838.1%)
Mutual labels:  validation, form
React Native Merlin
🧙 Simple web-like forms in react native.
Stars: ✭ 83 (+295.24%)
Mutual labels:  validation, form
Vue Formulate
⚡️ The easiest way to build forms with Vue.
Stars: ✭ 1,947 (+9171.43%)
Mutual labels:  validation, form
Legit
input validation framework
Stars: ✭ 81 (+285.71%)
Mutual labels:  validation, form
Form Validation.js
The most customizable validation framework for JavaScript.
Stars: ✭ 127 (+504.76%)
Mutual labels:  validation, form
Awesomevalidation
Android validation library which helps developer boil down the tedious work to three easy steps.
Stars: ✭ 1,093 (+5104.76%)
Mutual labels:  validation, form
Just Validate
Lightweight (~4,5kb gzip) form validation in Javascript Vanilla, without dependencies, with customizable rules (including remote validation), customizable messages and customizable submit form with ajax helper.
Stars: ✭ 74 (+252.38%)
Mutual labels:  validation, form
Simple React Validator
A simple react form validator inspired by Laravel validation.
Stars: ✭ 170 (+709.52%)
Mutual labels:  validation, form
Redux Form
A Higher Order Component using react-redux to keep form state in a Redux store
Stars: ✭ 12,597 (+59885.71%)
Mutual labels:  validation, form

Validation component for react-native forms

license npm version Build Status

Simple form validation component for React-Native inspired by formsy-react. Web version: react-material-ui-form-validator

Unfortunately I don't have Mac, so this library is tested only on Android.

Supported types:

  • TextInput

Default validation rules:

  • matchRegexp
  • isEmail
  • isEmpty
  • required
  • trim
  • isNumber
  • isFloat
  • isPositive
  • minNumber
  • maxNumber
  • minFloat
  • maxFloat
  • minStringLength
  • maxStringLength
  • isString

Some rules can accept extra parameter, example:

<TextValidator
   {...someProps}
   validators={['minNumber:0', 'maxNumber:255', 'matchRegexp:^[0-9]$']}
/>

Example

Usage

import React from 'react';
import { Button } from 'react-native';
import { Form, TextValidator } from 'react-native-validator-form';

class MyForm extends React.Component {
    state = {
        email: '',
    }

    handleChange = (email) => {
        this.setState({ email });
    }

    submit = () => {
        // your submit logic
    }

    handleSubmit = () => {
        this.refs.form.submit();
    }

    render() {
        const { email } = this.state;
        return (
            <Form
                ref="form"
                onSubmit={this.submit}
            >
                <TextValidator
                    name="email"
                    label="email"
                    validators={['required', 'isEmail']}
                    errorMessages={['This field is required', 'Email invalid']}
                    placeholder="Your email"
                    type="text"
                    keyboardType="email-address"
                    value={email}
                    onChangeText={this.handleChange}
                />
                 <Button
                    title="Submit"
                    onPress={this.handleSubmit}
                />
            </Form>
        );
    }
}

You can add your own rules

Form.addValidationRule('isPasswordMatch', (value) => {
    if (value !== this.state.user.password) {
        return false;
    }
    return true;
});

And remove them

componentWillUnmount() {
    Form.removeValidationRule('isPasswordMatch');
}

Usage

import React from 'react';
import { Button } from 'react-native';
import { Form, TextValidator } from 'react-native-validator-form';

class ResetPasswordForm extends React.Component {
    state = {
        user: {},
    }

    componentWillMount() {
        // custom rule will have name 'isPasswordMatch'
        Form.addValidationRule('isPasswordMatch', (value) => {
            if (value !== this.state.user.password) {
                return false;
            }
            return true;
        });
    }

    componentWillUnmount() {
        Form.removeValidationRule('isPasswordMatch');
    }

    handlePassword = (event) => {
        const { user } = this.state;
        user.password = event.nativeEvent.text;
        this.setState({ user });
    }

    handleRepeatPassword = (event) => {
        const { user } = this.state;
        user.repeatPassword = event.nativeEvent.text;
        this.setState({ user });
    }

    submit = () => {
        // your submit logic
    }

    handleSubmit = () => {
        this.refs.form.submit();
    }

    render() {
        const { user } = this.state;
        return (
            <Form
                ref="form"
                onSubmit={this.handleSubmit}
            >
                <TextValidator
                    name="password"
                    label="text"
                    secureTextEntry
                    validators={['required']}
                    errorMessages={['This field is required']}
                    type="text"
                    value={user.password}
                    onChange={this.handlePassword}
                />
                <TextValidator
                    name="repeatPassword"
                    label="text"
                    secureTextEntry
                    validators={['isPasswordMatch','required']}
                    errorMessages={['Password mismatch','This field is required']}
                    type="text"
                    value={user.repeatPassword}
                    onChange={this.handleRepeatPassword}
                />
                <Button
                    title="Submit"
                    onPress={this.handleSubmit}
                />
            </Form>
        );
    }
}
Advanced usage

API

Form

  • Props
Prop Required Type Default value Description
onSubmit true function Callback for form that fires when all validations are passed
instantValidate false bool true If true, form will be validated after each field change. If false, form will be validated only after clicking submit button.
onError false function Callback for form that fires when some of validations are not passed. It will return array of elements which not valid.
debounceTime false number 0 Debounce time for validation i.e. your validation will run after debounceTime ms when you stop changing your input
  • Methods
Name Params Return Descriptipon
resetValidations Reset validation messages for all validated inputs
isFormValid dryRun: bool (default true) Promise Get form validation state in a Promise (true if whole form is valid). Run with dryRun = false to show validation errors on form

All validated fields (Input)

  • Props
Prop Required Type Default value Description
validators false array Array of validators. See list of default validators above.
errorMessages false array Array of error messages. Order of messages should be the same as validators prop.
name true string Name of input
errorStyle false object { container: { top: 0, left: 0, position: 'absolute' }, text: { color: 'red' }, underlineValidColor: 'gray', underlineInvalidColor: 'red' } } Error styles
validatorListener false function It triggers after each validation. It will return true or false
withRequiredValidator false bool Allow to use required validator in any validation trigger, not only form submit
  • Methods
Name Params Return Descriptipon
getErrorMessage Get error validation message
validate value: any, includeRequired: bool Run validation for current component
isValid bool Return current validation state
makeInvalid Set invalid validation state
makeValid Set valid validation state

Contributing

This component covers all my needs, but feel free to contribute.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].