You can perform custom validations in 3 different ways, depending on your needs:
The validation key “regexp” can be used to perform custom validations based on a specified regular expression and the flags (specified separately) must be in string format (for use with RegExp object constructor):
<input name="user" required data-dj-validator="regexp,^[aeiou]+$,i">
If the regular expression has commas ( reserved character ), they must be replaced by the ° character (degree) to avoid errors:
<input name="user" required data-dj-validator="regexp,^[aeiou]{3°5}$,i">
The call validation key allows validation by executing a user defined function, this function receives the field to be validated and must return true or false:
<input name="textinput" type="text" data-dj-validator="call,isUpperCase">
Being the user function:
function isUpperCase($field){
value=$field.val();
if(value==value.toUpperCase())return true;
else return false;
}
The global addDjvalidator function allows add a new custom validation key within the plugin:
$.fn.djValidator.add(key_name,message,callback);
Where:
key_name: Is the validation key that will be created
message: Is the validation message to be used
callback: Validation function to be executed, receives 2 parameters, the field to be validated "field" and the parameters "params" , these parameters are an array that starts at 1, since 0 is the validation key.
Example:
creating the “even” validator, which will validate if the number entered is even, also with a parameter so that it does not exceed a maximum value:
$.fn.djValidator.add('even','only even numbers.',
function($field,params){
value=parseInt($field.val());
max= params[1];
if(value%2!=1||value>max)return false;
else return true;
}
);
Then it can be used like any other plugin key:
<input type="text" data-dj-validator="even,10" required>