JSPM

  • Created
  • Published
  • Downloads 59760
  • Score
    100M100P100Q141449F

A constructor function for data validation on both the server and client, inspired by the Laravel PHP framework's Validator class.

Package Exports

  • validatorjs/dist/validator.min.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (validatorjs) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

validatorjs v0.7.0

Build Status

Setup

Browser:

  1. Include validator.min.js script onto your page from the distribution folder dist.
  2. Invoke the Validator constructor function. See below for details on Validator parameters and validation rules.

Node.js:

Install the Validator package from the NPM registry https://npmjs.org/package/validatorjs

    npm install validatorjs
    var Validator = require('validatorjs');

Usage and Examples

The 1st argument to the constructor is an object that contains the data you want to validate.

The 2nd argument is an object that contains the validation rules.

Example 1:

    var data = {
        email: 'johndoe@gmail.com'
    };
    
    var rules = {
        email: 'email'
    };

    var validation = new Validator(data, rules);
    
    validation.passes() // true
    validation.fails() // false
    

To apply validation rules to the data object, use the same object key names for the rules object.

Example 2:

    var rules = {
        name: 'required|size:3',
        email: 'required|email'
    };

    var data = {
        name: '',
        email: ''
    };

    var validation = new Validator(data, rules);

    validation.fails(); // true

Validation Rules

####alpha

The field under validation must be entirely alphabetic characters.

####alpha_dash

The field under validation may have alpha-numeric characters, as well as dashes and underscores.

####alpha_num

The field under validation must be entirely alpha-numeric characters.

####email

The field under validation must be formatted as an e-mail address.

    address: 'email'

####min:value

Validate that an attribute is at least a given size.

    payment: 'min:10'

####max:value

Validate that an attribute is no greater than a given size

    cost: 'max:100'

Note: All minimum and maximum checks are inclusive.

####numeric

Validate that an attribute is numeric. The string representation of a number will pass.

    age: 'numeric'

####required

Checks if the length of the String representation of the value is >

    username: 'required'

####same:field

The given field must match the field under validation.

####size:value

Validate that an attribute is a given length, or, if an attribute is numeric, is a given value

    duration: 'size:2'

####url

Validate that an attribute has a valid URL format

    link: 'url'

Error Messages

This contructor will automatically generate error messages for validation rules that failed.

If there are errors, the Validator instance will have its errors property object populated with the error messages for all failing attributes. The methods and properties on the errors property object are:

####.first(attribute)

returns the first error message for an attribute, false otherwise

####.get(attribute) returns an array of error messages for an attribute, or an empty array if there are no errors

####.errorCount

the number of validation errors

####Example:

    var validation = new Validator(input, rules);
    validation.errors.first('email'); // returns first error message for email attribute
    validator.errors.get('email'); // returns an array of error messages for the email attribute

Here are the default error messages. If you want to change these error messages, modify the file in the src directory.

    var messages = {
        alpha: 'The :attribute field must contain only alphabetic characters.',
        alpha_dash: 'The :attribute field may only contain alpha-numeric characters, as well as dashes and underscores.',
        alpha_num: 'The :attribute field must be alphanumeric.',
        required: 'The :attribute field is required.',
        email: 'The :attribute format is invalid.',
        def: 'The :attribute attribute has errors.',
        min: {
            numeric: 'The :attribute must be at least :min.',
            string: 'The :attribute must be at least :min characters.'
        },
        max: {
            numeric: 'The :attribute must be less than :max.',
            string: 'The :attribute must be less than :max characters.'
        },
        same: 'The :attribute and :same fields must match.',
        size: {
            numeric: 'The :attribute must be :size.',
            string: 'The :attribute must be :size characters.'
        },
        numeric: 'The :attribute must be a number.',
        url: 'The :attribute format is invalid.'
    };

Public Instance Methods

####.passes() returns boolean

####.fails() returns boolean

####.first(attribute_name) returns first error message for string attribute_name, or null if no error message exists.

This will be deprecated in the future. Use validation_instance.errors.first(attribute) instead

Static Methods

####.register(custom_rule_name, callbackFn, errorMessage)

Register a custom validation rule

  • custom_rule_name - string
  • callbackFn - function. If callbackFn returns a truthy value, the validation will pass for this rule. Otherwise, this validation rule will fail.
  • errorMessage is an optional string where you can specify a custom error message. :attribute inside errorMessage will be replaced with the attribute name.
    Validator.register('telephone', function(val) {
        return val.match(/^\d{3}-\d{3}-\d{4}$/);
    }, 'The :attribute phone number is not in the format XXX-XXX-XXXX.');

Testing

Install node module dependencies

    npm install

See SpecRunner.html for Jasmine tests in the browser.

You can also run the jasmine tests via Node.js once you've installed the NPM package jasmine-node.

    jasmine-node spec/ --verbose --color

    //OR

    npm test (which calls the above command)

Once the above test passes, run the following command which will in turn run JSHint and minify the source

    grunt