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