From now on, use simple and elegant validata.js

Source: Internet
Author: User

Form verification, is the background of the development of the constant topic, after a lot of practical experience found

The use of elegant and convenient validate.js for verification is a very pleasant thing.

<form data-validate>    Enter:    <input type= "password" data-rule= "Password: required;number[Please enter an integer];equals (target) "/>    <input type=" password "id=" target "/> </form>


The data-validate tag is added to the form element in the instance, and Validate.js automatically validates the validation control within the form tag.
The validation control uses the Data-rule tag, and the Data-rule syntax format is:

Hint Name: rule (rule parameter) [custom hint];

WHERE ' hint name ' and ' custom hint ' are optional, some rules need to pass in a parameter, such as the ID of the equals rule that needs to pass in the comparison control

The {name} and {arg} variables can be used in custom hints, which are useful in custom rules

Validate.js will add a label after the validation control <div class= "Error_msg" ></div> the label's template in $. Validate. in Errortemplate

The tagging operation is implemented by Validatehandler, and the Validate.js has a simple post-validation action handler function

Users can use Window.validatehandler = function (msg, scrollTo) to their own validation post-processing function (add hint tag, scroll to error control)

Validate.js has built-in rules, defined under $.validate.rules, with the following structure:

$.validate.rules = {       required: {rule:/./, message: ' cannot be null '},       equals: {            action:function (el, Rul    E, Val, target) {   return val!== $ (' # ' +target). Val ();            }, message: ' Two input values are inconsistent '       }}    


Users can add custom rules to $.validate.rules, and the rules contain rule,action,message
The rule is expressed as a regular rule, and the action represents a function validation rule, both of which cannot have both
The message can be a "string" or a function, and if it is a "string" type, you can use {Name},{arg} to replace the control name with the parameter

If it is a function, its format is message:function (name,arg) {return '}



Use JS to get control validation status:

$ (' [Data-rule] '). Isvalidate (), BOOL


Get the validation message:

$ (' form '). Validate (Justtest,scrollto)-    param:justtest bool Just check, do not modify DOM    param:scrollto bool Scroll to error element   return {isvalidate:bool,  messages:[{      element:jqueryelement,       message:string       }]     }                

From now on, use simple and elegant validata.js

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.