Jquery. validate. js plugin experience record

Source: Internet
Author: User

Recently, the jQuery. validate. js plug-in is required for the project, so you can record some of your work experience for future study.

[Example]

1. Foreground page

<Form id = "form1" method = "post"> UserName: <input type = "text" id = "UserName" name = "UserName"/> <! -- <Span id = "name_error"> </span> --> <br/> password: <input type = "password" id = "Password" name = "Password"/> <br/> <input type = "submit" id = "btnRegister" name = "btnRegister" value = "register"/> <input type = "button" id = "btnCancel" name = "btnCancel" value = "cancel"/> </form>

2. Javascript

<Script type = "text/javascript"> // Add custom validation jQuery. validator. addMethod ("checkPWD", function (value, element) {var flag = false; // flag = chkpwd ($ ("# pwd ")); // custom password verification logic return this. optional (element) | flag;}); $ (function () {$ ("# form1 "). validate ({rules: {UserName: {required: true, // emote: function () {// verify whether the user name exists in method 1 // $. ajax ({// type: 'post', // url: "Handler/Handler1.ashx", // data :{ Name: $ ("# UserName "). val ()}, // async: false, // success: function (data) {// $ ("# name_error" ).html (data ); ///} //}); //} remote: {// verify whether the user name exists. method 2 type: "POST", url: "Handler/Handler1.ashx ", // The background processing program dataType: "json", // receives the data format data :{// the parameter action to be passed: function () {return "CheckName"}, name: function () {return $ ("# UserName "). val () ;}, param1: function () {return "Custom parameter 1" ;}}}, Pas Sword: {required: true, checkPWD: true // custom verification. If you do not need custom verification, change true to false. }}, Messages: {"UserName": {required: "<span> the user name cannot be blank. </span>", remote: "<span> the member name already exists! </Span> "}," Password ": {required:" <span> the Password cannot be blank. </span> ", checkPWD:" <span> the password does not comply with the custom rules... </Span> "}}});}) </script>


3. background processing program

Public void ProcessRequest (HttpContext context) {context. response. contentType = "text/plain"; string action = context. request ["action"]; // obtain the username and custom parameter string name = context. request ["name"]; string param1 = context. request ["param1"]; if (! String. isNullOrEmpty (name) {// simulate database query. if the user inputs admin, the system prompts that the account already exists and returns false if (name. equals ("admin") {context. response. write ("false");} else {context. response. write ("true ");}}}

4. Running result

4.1 If the user does not enter data, an error is returned:

4.2 When an error message is entered, the following error occurs:

Related Article

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.