Work Summary: How to use the Validate of jquery

Source: Internet
Author: User

Preface : After a series of investigation, finally clarified the jqgrid of the validate of the remote usage, very powerful, and very convenient, below some summary

Step one: Prepare the material jquery.validate.js and load it into your project

<script src= "Js/jquery.validate.js" type= "Text/javascript" ></script>
The second step: First look at the remote source code, you will understand a lot, I was through the Firebug investigation to find out, the key points of the comments written out
Http://docs.jquery.com/Plugins/Validation/Methods/remoteremote:function (value, element, param) {if ( This.optional (Element)) {return "Dependency-mismatch";} has been verified, will take var previous = This.previousvalue (element) from this, if (!this.settings.messages[element.name]) { This.settings.messages[element.name] = {};} Previous.originalmessage = This.settings.messages[element.name].remote;this.settings.messages[element.name]. Remote = Previous.message;param = typeof param = = = = "string" && {Url:param} | | Param;if (Previous.old = = = value) {return previous.valid;} Previous.old = Value;var Validator = this;this.startrequest (Element), var data = {};d ata[element.name] = Value;$.ajax ($. Extend (True, {url:param,//passes the background URL for data validation mode: "Abort", Port: "Validate" + Element.name,datatype: "JSON", data:data,/ /pass the parameter for the corresponding Intput label corresponding to the name and value Success:function (response) {//response return data for the background, error message when wrong, The right time to return truevalidator.settings.messages[element.name].remote = previous.originalmessage;var valid = ResponSe = = = True | | Response = = = "true";//If it returns True, it goes to the IF statement, and the error message disappears if (valid) {var submitted = validator.formsubmitted; Validator.prepareelement (Element), validator.formsubmitted = Submitted;validator.successlist.push (Element);d elete Validator.invalid[element.name];validator.showerrors ();} else {var errors = {};var message = Response | | Validator.defaultmessage (ELEMENT, "remote");//If an error message is returned in the background, the default display information is not displayed ER Rors[element.name] = Previous.message = $.isfunction (message)? Message (value): message;validator.invalid[element.name] = true;validator.showerrors (errors);} Previous.valid = valid;validator.stoprequest (element, valid);}, param)); return "Pending";}
The third step: remote setup on the front end, the request address of the background pass past
<input type= "text" class= "required alphanumeric" remote= "${ctx}/jade/checkcodeisexist.do" name= "code"/>
Fourth step: Receive parameters in the background processing, return the corresponding processing information, note that normal time to return true, the test does not pass when the error message is returned
@RequestMapping (value = "checkcodeisexist") public void Checkcodeisexist (HttpServletRequest request, HttpServletResponse reponse) throws Exception {Jadeinfo jadeinfo = This.jadeinfoDAO.selectByCodeKey ( Strutil.getutf8string (Request.getparameter ("Code"))); if (jadeinfo! = null && jadeinfo.getdeleteflag () = = 0) { Out ("The commodity code already exists! ", reponse);} else {out (true, reponse);}}
Summary: OK, the above steps to complete the use of remote, very simple and refreshing, but I did not read the source and back to try many times, are not very useful to see the source is still worth doing. All right, upload one.



Work Summary: How to use the Validate of jquery

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.