laravel-validator-for-js

moved to validatorjs - https://npmjs.org/package/validatorjs

A constructor function for data validation on both the server and client, inspired by the Laravel PHP framework. The API is very similar to that of the Laravel 3 Validator class.

npm install laravel-validator-for-js
18 downloads in the last week
36 downloads in the last month

validatorjs

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/laravel-validator-for-js

    npm install laravel-validator-for-js
    var Validator = require('laravel-validator-for-js');

Since version 0.4, the Validator constructor is immediately returned from the require call. I.e. You no longer have to do require('laravel-validator-for-js').Validator

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 input 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

  • required - Checks if the length of the String representation of the value is > 0
    username: 'required'
  • email - Checks for an @ symbol followed by a period
    address: 'email'
  • size - Validate that an attribute is a given length, or, if an attribute is numeric, is a given value
    duration: 'size:2'
  • min - Validate that an attribute is at least a given size.
    payment: 'min:10'
  • max - 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'
  • url - Validate that an attribute is a URL
    link: 'url'

Error Messages

This contructor will automatically generate error messages for validation rules that failed. You can use the first method to fetch the first error message of a failing attribute. You can access all of the errors through the errors property on the Validator instance.

There is also an errorCount property on the validation instance to specify the number of validation errors.

Public Instance Methods

  • passes() - returns boolean
  • fails() - returns boolean
  • first(attributename) - returns first error message for _string attributename, or _null if no error message exists

Static Methods

  • register(custom_rule_name, callbackFn, errorMessage) - register a custom validation rule.

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
npm loves you