fields-validator

0.1.3 • Public • Published

fields-validator

Build Status

a simple validator for form fields

Install

    npm install fields-validator --save

Usage

var fieldsValidator = require('fields-validator');

var validateConfig = {
    name: [
        ['required', 'input name please']
    ],
    password: [
        ['minLength', 6, 'too short'],
        ['maxLength', 11, 'too long']
    ],
    rePassword: [
        ['custom', function(data) {
            if (data.password === '' ||
                data.rePassword === '' ||
                data.password === data.rePassword
            ) {
                return true;
            }
            return false;
        }, 'should be equal to password']
    ],
    email: [
        ['regexp', /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i, 'input a right mail please'],
    ]
};

var formData = {
    name: '',
    password: 'abc',
    rePassword: 'abcd',
    email: 'abc@gmail.com'
};

var ret = fieldsValidator(validateConfig, formData);
console.log(ret);
// output
// [ { field: 'name', errMsg: 'input name please' },
//   { field: 'password', errMsg: 'too short' },
//   { field: 'rePassword', errMsg: 'should be equal to password' } ]

Package Sidebar

Install

npm i fields-validator

Weekly Downloads

0

Version

0.1.3

License

MIT

Unpacked Size

11.4 kB

Total Files

7

Last publish

Collaborators

  • viz