node package manager


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.


  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.

Install the Validator package from the NPM registry

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

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.

var data = {
email: ''
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.

var rules = {
name: 'required|size:3',
email: 'required|email'
var data = {
name: '',
email: ''
var validation = new Validator(data, rules);
validation.fails(); // true 
  • 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'

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.

  • passes() - returns boolean
  • fails() - returns boolean
  • first(attribute_name) - returns first error message for string attribute_name, or null if no error message exists
  • 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.');

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