Sets jqxValidator rules. Format of a single rule is as follows:
{ input: 'selector-of-the-input',
message: 'Custom message on error',
action: 'Custom action (keyup, change...etc)',
rule: 'Build rule (ssn, phone, email...) or custom function',
position: 'Position of the hint (format pos:x,y)',
hintRender: 'Function for hint rendering' }
Let's look at all different properties of a single rule.
The input property must be selector of the input you want to validate (we recommend to use ids - example: '#userInput').
The message property is the custom
message which will popup, on validation error, for the current rule.
Action is a string which is the event on which you want to validate the input (for example click, mouseup, blur, keyup...).
The rule property is defining the way you want to validate the input.
In jqxValidator there are
built in rules like: 'ssn', 'email', 'required', 'phone', 'zipCode', 'maxLength=len', 'minLength=len', 'length=max,min'. In the last three validation rules the strings after the "=" are the rule parameters, for example: 'maxLength=13'. You can also write a function
for a custom rule.
'ssn' - 'Social Security Number' Requires input like: ___-__-____
'email' - requires valid e-mail address.
'required' - requires a CheckBox or Radio Button to be checked or any value to be entered in an Input.
'phone' - requires input like: (___)___-____
'zipCode' - requires a valid zip code like: ___-__-____
'maxLength=len' - restricts the maximum input characters to 'len'.
'minLength=len' - restricts the minimum input characters to 'len'
'length=min,max' - restricts the input length to a specific range.
Hint positions are as follows: 'left', 'right', 'top', 'bottom', 'bottomcenter', 'topcenter', 'topleft', 'topright', 'bottomleft', 'bottomright'. If you wish to set also an offset you can pass the position like: 'topleft:15,3'. This is going to position your
message popup in top-left of the input with offset: left - 15px, top - 3px.
The last property is hintRender. This is function used for hint rendering. If you don't pass one the default is going to be used. Notice that the position and hintRender are optional. If you don't set them the default values are going to be used.
Code examples
Initialize a jqxValidator with the rules
property specified.
$('#form').jqxValidator( { rules: [{ input: '#passwordInput',
message: 'The password is required!',
action: 'keyup',
rule: 'required' },
{ input: '#passwordInput',
message: 'Your password must be between 4 and 12 characters!',
action: 'keyup',
rule: 'length=4,12' }] } );
Custom Rule Definition. The function returns true or false depending on whether the input is correct or not.
{ input: '#birthInput', message: 'Your birth date must be between 1/1/1900 and 1/1/2012.', action: 'valuechanged', rule: function () {
var date = $('#birthInput').jqxDateTimeInput('value');
var result = date.dateTime.getFullYear() >= 1900 && date.dateTime.getFullYear() <= 2012;
return result;
}
Set the hintRender property of a rule.
$('#sendButton').on('click', function () {
$('#testForm').jqxValidator('validate');
});
var that = this;
var render = function (message, input) {
if (that._message) {
that._message.remove();
}
that._message = $("<span style='background: red; color: white;'>" + message + "</span>
")
that._message.appendTo($(document.body));
return that._message;
}
$('#testForm').jqxValidator({
rules: [
{ input: '#userInput', message: 'Username is required!', action: 'keyup, blur', rule: 'required', hintRender: render },
{ input: '#userInput', message: 'Your username must be between 3 and 12 characters!', action: 'keyup, blur', rule: 'length=3,12', hintRender: render }
]});