JQuery Validate plug-in Validation form usage examples

Source: Internet
Author: User

Previously used jquery validator to do some projects, but recently picked up, discovery features upgraded, some of the previous use of the method is already in the error, and then rearrange a blog to summarize the latest jquery form validate use tutorial.

How to use JQuery Validate

We often ask for user form submission events, validation events after loss of focus on form elements, methods of error display, validation of form rules, and validation of messages for form rules.

The code is as follows Copy Code

JQuery (document). Ready (function ($) {
  $ ("#cf-order-checkout-form"). Validate ({
   / /Custom handler to call named function "Do_payment"
    submithandler:function (form) {
 &nb sp;    $ (form). attr ("target", "_blank");
      form.submit ();
   },
   //Www.111cn.net Validate the previously selected element when the user Clicks out
    onfocusout:function (Element) {
      $ (element). Valid ();
   },

Errorplacement:function (Error, Element) {
if (element.attr (' type ') = = ' Radio ') {
Error.appendto (Element.parent (). parent ());
}
else {
Error.appendto (Element.parent ());
}
},

Validation rules
Rules: {
' Address[province] ': {required:true},
' address[city] ': {required:true},
' Address[county] ': {required:true},
' Transaction[payment] ': {required:true}
},
Validation messages
Messages: {
' Address[province] ': {
Required: "Please select your province or autonomous region." "
},
' address[city] ': {
Required: "Please select your city, region or autonomous prefecture." "
},
' Address[county] ': {
Required: "Please select your district or county." "
},
' Transaction[payment] ': {
Required: "Please choose a payment method." "
}
}
});
});

For other uses, see the official API functions.

JQuery Validate Submithandler causes dead loops


To prevent form submission death loops, you should use Form.submit () rather than $ (form). Submit ();

The Submithandler form variable converts the Dom,dom object into a jquery object, plus $ (), and then it can be manipulated using jquery syntax.

Correct use of the Submit form method:

The code is as follows Copy Code

Submithandler:function (form) {
Form.submit ();
}

That's right.

JQuery Validate Ajax Submit
We can define the Submithandler function using jquery ajax to request processing.

The code is as follows Copy Code
$ ("#user-login"). Validate ({
Custom handler to call named function "Do_payment"
Submithandler:function (form) {
$.ajax ({
Type: "POST",
URL: '/ajax/user/login ',
Data: {"name": $ (form). Find (' #edit-name '). Val (), "Pass": $ (form). Find (' #edit-pass '). Val ()},
DataType: "JSON",
Success:function (data) {
if (Data.result) {
Location.reload ();
}
else if (data.message) {
alert (data.message);
}
else {
Alert (' password error ');
}
}
});
},
Validate the previously selected element when the user clicks out
Onfocusout:function (Element) {
$ (Element). valid ();
},
Errorplacement:function (Error, Element) {
Error.appendto (Element.parent ());
}
});

Finally have to thank the following jquery Validate plugin author.

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.