Skip to content
Jony edited this page Jan 27, 2016 · 9 revisions

The following API description, using the $form refers to form elements jQuery object.


Plugin Methods

$form.validator( options )

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
            }
        });
    }
});

$form.validator( validCallback )

Same as:

$('#form1').validator({
    valid: validCallback
});

$input.isValid( callback, checkOnly )

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
} 

$area.isValid( callback, checkOnly )

Check whether an area is valid.
@param {Function} callback
@return {Boolean | undefined}
@example:

$('#formId').isValid(function(v){
    console.log(v ? 'valid' : 'invalid');
});

$form.data( "validator" )

Get form validation instance. @example:

var v = $('form').validator().data('validator');
v.showMsg('#mobile', 'Please fill phone number.');

Instance Methods

instance.test( element, ruleName )

Test whether the element's value is in line with ruleName.
Note:

  1. Because this method returns a Boolean value, so can only be called by way of instance.
  2. 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)"
    }
});

instance.showMsg( element, obj )

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."
});

instance.hideMsg( element [, obj] )

Hide the message of a field element.
@param {Element | jqSelector}
@param {Object}
@example

$('#form1').validator('hideMsg', '#mobile');
// or
$('#form1').data('validator').hideMsg('#mobile');

instance.setField( key, field )

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);

instance.cleanUp()

Clear verification messages in the form.

instance.destroy()

Destroy form validation.

instance.holdSubmit(hold)

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);
            }
        });
    }
});

Static Sethods

$.validator.config( options )

Global Configuration Interface.

$.validator.setTheme( options )

Setting the theme parameters, see configuration files from local folder.