jsdv
JavaScript forms client validator. Created for html attributes validation declarations
Demo: http://evsegneev.pp.ua/jsdv Npm: https://www.npmjs.org/package/jsdv
Browser Support:
- IE8+
- Opera 10.6+ (maybe earlier, don't know)
- Safari 4.0+
- Firefox 3.0+
- Chrome 14+ (maybe earlier, don't know)
How it works?
-
Include validator.js and validator.css to your html.
-
Declare your validation:
- Initialize validator:
var v = ;//here you can add your own validation rulesv;
That's all!
To check that all fields on page is valid, use v.isValid();
You can pass id of your own error container:
Now there are validators:
- required
- digits
- letters
- url
- length-min
- length-max
- same
- [standalone] ajax
Length-min and length-max validate needs an extra attributes: validate-length-min and validate-length-max:
Same validator an requires extra-attributes: validate-with and validate-on. To use it, you only need to declare it on one of the compared elements
Validate-on attribute defines an events that fires validation. First word is id of the element, and second - event without leading "on". Separate different events by comma. Next declaration tolds that validation will be executed after "onkeyup" event on elements with id="pass-first" and id="pass-second":
validate-on="pass-first keyup, pass-second keyup"
You can put more than one validator on form input:
Ajax validator works differently (for now, I think). You can't use ajax with other validators. Example:
Server takes $_POST['validate-data']
- your input value. And you can do anything with it.
You must return "true" for valid or any other value for invalid
If you want to add custom validator, you need to use an extend function. Function extend accepts an object. Object must contain two properties:
- f - validation function
- name - name of the validation function
In context of the validation function,
o
is an html element that we wants to validate, and_v
- built-in validation function, or extender. Validation function must return boolean value. If it returns true, validation passed. Else - validation not passed.
var v = ; v; v; console;
There is good extender example - 'depends-test' validator. It selects validation rule based on another input value. If input#depend-test value equals 'secret', then digits only valid, else letters valid.
Html:
JavaScript:
v;