-
Notifications
You must be signed in to change notification settings - Fork 146
Methods
The following API description, using the $form
refers to form elements jQuery object.
Initialization form validation.
@param
{Object} options
@return
{jqObject}
@example
$('#form1').validator({
rules: {
username: function(element, params){
return this.test(element, 'email')
|| this.test(element, 'mobile')
|| 'You can enter user name or phone number';
}
},
messages: {
required: "Please fill {0}",
email: "E-mail address is not valid"
},
fields: {
username: 'required; username;',
password: 'required; length[6~16]',
email: 'email;'
},
valid: function(form){
$.ajax({
url: "result.php",
data: $(form).serialize(),
success: function(d){
//do something
}
});
}
});
Same as:
$('#form1').validator({
valid: validCallback
});
Check whether a field is valid.
@param
{Function} callback
@param
{Boolean} checkOnly , set true to prevent show error message
@return
{Boolean | undefined}
@example
:
$('#mobile').isValid(function(){
// field is valid
// some code
});
// not show error message
$('#mobile').isValid(function(){
// field is valid
// some code
}, true);
// using a callback to obtain validation results.
$('#mobile').isValid(function(result){
if (result === true) {
// do something
}
else {
}
});
// If there are no ajax validation.
// You can also use the following method.
if ( $('#mobile').isValid() ) {
// do something
}
Check whether an area is valid.
@param
{Function} callback
@return
{Boolean | undefined}
@example
:
$('#formId').isValid(function(v){
console.log(v ? 'valid' : 'invalid');
});
Get form validation instance.
@example
:
var v = $('form').validator().data('validator');
v.showMsg('#mobile', 'Please fill phone number.');
Test whether the element's value is in line with ruleName.
Note:
- Because this method returns a Boolean value, so can only be called by way of instance.
- This method can not test remote rules, and other asynchronous validation rules.
@param
{Element | jqSelector}
@param
{String}
@return
{Boolean}
@example
$("#myForm").validator({
rules: {
loginName: function(element) {
return /^[a-zA-Z]\w{3,}/.test(element.value)
|| this.test(element, "mobile")
|| this.test(element, "email")
|| 'Please fill user name, phone number or E-mail';
}
},
fields: {
"username": "required; loginName",
"password": "required; length(6~16)"
}
});
A message is displayed on a field element.
@param
{Element | jqSelector}
@param
{Object}
@example
$('form').validator('showMsg', '#mobile', {
type: "error",
msg: "Phone number already exists."
});
Hide the message of a field element.
@param
{Element | jqSelector}
@param
{Object}
@example
$('#form1').validator('hideMsg', '#mobile');
// or
$('#form1').data('validator').hideMsg('#mobile');
Updated field parameter.
- If it is a new field is equivalent to adding a field;
- If
field === null
, the field will be deleted (not verified key field).
@param
{String}
@param
{Object}
@example
$('form').validator("setField", "username", "required;");
// Remove the field's verification.
$('form').validator("setField", "username", null);
Clear verification messages in the form.
Destroy form validation.
To prohibit or releases the submit event.
Note:
If you hold the form, has not released the hold, this is equivalent to only submit once.
@param
{Boolean}
@example
$("#myForm").validator({
valid: function(form){
var me = this;
// Before submitting the form, hold form, to prevent duplicate submission.
me.holdSubmit();
$.ajax({
url: "xxx.php",
data: $(form).serialize(),
type: "POST",
success: function(){
// After the form is submitted successfully, release hold.
me.holdSubmit(false);
}
});
}
});
Global Configuration Interface.
Setting the theme parameters, see configuration files from local
folder.