jquery.validate1.9.0 Front desk verification using

Source: Internet
Author: User

I. Using the Jquery.form plugin to submit a form method to use the Jquery.validate plugin

Behavior: When a form is submitted, the form is submitted as usual, even if the foreground is not validated.

Workaround:

[PHP]View Plaincopy
  1. $ (' #myForm '). Submit (function () {
  2. if (!$ (this). Valid ()) return false; Plus this sentence ok
  3. $ ('. Error '). html (');
  4. $ ("#go"). Prop ("disabled", true);
  5. $ (this). Ajaxsubmit ({
  6. Type:"POST",
  7. //beforesubmit:showrequest,
  8. DataType:' json ',
  9. Success:showresponse
  10. });
  11. return false;
  12. });

Related instructions:

Custom Submission Methods (Ajax submissions)
If you submit using AJAX, then use the following two ways to combine with the calibration framework
1), using the Submithandler property to configure the AJAX submission, Submithandler: When the form is fully validated, the configured code is called back, which is where the Ajax commit is invoked after the checksum is passed.
2), use the valid method, listen to the form's submit event, when $ (' #form '). Valid () returns True when it is submitted again.

The form is Ajax submitted by listening to the submit event of the form. The complete sample code is as follows:

[PHP]View Plaincopy
  1. $ (document). Ready (function () {
  2. $ (' #myForm '). Submit (function () {
  3. if (!$ (this). Valid ()) return false;
  4. $ ('. Error '). html (');
  5. $ ("#go"). Prop ("disabled", true);
  6. $ (this). Ajaxsubmit ({
  7. Type:"POST",
  8. //beforesubmit:showrequest,
  9. DataType:' json ',
  10. Success:showresponse
  11. });
  12. return false;
  13. });
  14. var validator = $ ("#myForm"). Validate ({
  15. Rules: {
  16. Username: "required",
  17. Email: {
  18. Required:true,
  19. Email:true
  20. }
  21. },
  22. Messages: {
  23. Username: "Please enter your name",
  24. Email: {
  25. Required: "Please enter your email address",
  26. Email: "Please enter the correct email address"
  27. }
  28. }
  29. });
  30. });
  31. function Showresponse (jsondata,statustext)
  32. {
  33. if (statustext==' success ')
  34. {
  35. $ ("#go"). Prop ("disabled", false);
  36. if (jsondata.status = = 1)
  37. {
  38. $ ("#return"). HTML (jsondata.message);
  39. }
  40. Else
  41. {
  42. $.each (Jsondata.errors, function (k,v) {
  43. //$ (' #output '). Find (' ul '). Append (' <li> ' + V + ' </li> ');
  44. $ ('. E_ ' + k). html (v);
  45. });
  46. }
  47. }
  48. }
Second, the method of controlling the error information location

Phenomenon One:

I added a new verification code to the registration form. When the validation result is wrong, the error message goes in front of the verification code. As shown in the following:

Purpose: Make the error message behind the verification code

Phenomenon Two:

The red hint in the content, I want to move to (* must fill) the back.

The above two phenomena can be used by the jquery.validate to control the location of the error message-' errorplacement ', the use is also very convenient:

[PHP]View Plaincopy
    1. Errorplacement: function (Error, Element)
    2. {
    3. Error.appendto (Element.parent ());
    4. }



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.