An easy and powerful string-based validation library.
A PHP version, Pupil.php, is planned.
dist/Pupil.min.js and include it on your page.
Install the module:
npm install pupil
And then require it in your project:
var pupil = require'pupil';
The basic syntax is this:
values are objects with matching keys. The rules are specified as
rule strings; more information on those below.
var rules =name: 'min:3 && max:8'country: 'min:2';var values =name: nameInputElemvaluecountry: countryInputElemvalue;
The two objects don't have to have identical keys, but values without a matching key in rules won't be evaluated at all.
validate() method returns an object like this:
name: true // Name passed validationcountry: false // Country didn't
Rule strings are Pupil's primary method of specifying validation rules.
Logical operators (
! (not)) and "blocks" (
)) have an identical syntax to most
programming languages, but functions are used like this:
the name of the function and
arg2 are the arguments for it.
For each validation function, there is also a matching function prepended by
other that allows you to run functions
on other values than the one the rule string is for. This can be useful for fields that have differing requirements depending on another field. For example:
state: 'minLen:2 || ( ! otherEquals:country,US && minLen:0)'
The rule strings can also be nested indefinitely by using blocks.
The following example would require a state name of at least 2 characters for those who have chosen US or CA as their country.
state: 'minLen:2 || (( ! otherEquals:country,US && ! otherEquals:country,CA) && minLen:0)'