laravel-validator-for-js
v0.2.0 A port of the Validator class in the Laravel PHP MVC framework to JavaScript
##validator.js
Setup
Browser usage:
- Include validator.js or validator.min.js script onto your page from the distribution folder (dist).
- Invoke the Validator constructor function. See below for details on Validator parameters and validation rules.
Node.js Usage:
- Install the Validator package from the NPM registry https://npmjs.org/package/laravel-validator-for-js
npm install laravel-validator-for-js
- On your node.js script, require the module.
var ValidationModule = require('laravel-validator-for-js');
var validation = new ValidationModule.Validator({
name: 'John',
age: 45,
email: 'johndoe.gmail.com'
}, {
name: 'required',
age: 'numeric',
email: 'required|email'
});
- Invoke the Validator constructor function.
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: '[email protected]'
};
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'
- numeric - Validate that an attribute is numeric. The string representation of a number will pass.
age: 'numeric'
Note: All minimum and maximum checks are inclusive.
###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
See SpecRunner.html for Jasmine tests and examples