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
- $ (' #myForm '). Submit (function () {
- if (!$ (this). Valid ()) return false; Plus this sentence ok
- $ ('. Error '). html (');
- $ ("#go"). Prop ("disabled", true);
- $ (this). Ajaxsubmit ({
- Type:"POST",
- //beforesubmit:showrequest,
- DataType:' json ',
- Success:showresponse
- });
- return false;
- });
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
- $ (document). Ready (function () {
- $ (' #myForm '). Submit (function () {
- if (!$ (this). Valid ()) return false;
- $ ('. Error '). html (');
- $ ("#go"). Prop ("disabled", true);
- $ (this). Ajaxsubmit ({
- Type:"POST",
- //beforesubmit:showrequest,
- DataType:' json ',
- Success:showresponse
- });
- return false;
- });
- var validator = $ ("#myForm"). Validate ({
- Rules: {
- Username: "required",
- Email: {
- Required:true,
- Email:true
- }
- },
- Messages: {
- Username: "Please enter your name",
- Email: {
- Required: "Please enter your email address",
- Email: "Please enter the correct email address"
- }
- }
- });
- });
- function Showresponse (jsondata,statustext)
- {
- if (statustext==' success ')
- {
- $ ("#go"). Prop ("disabled", false);
- if (jsondata.status = = 1)
- {
- $ ("#return"). HTML (jsondata.message);
- }
- Else
- {
- $.each (Jsondata.errors, function (k,v) {
- //$ (' #output '). Find (' ul '). Append (' <li> ' + V + ' </li> ');
- $ ('. E_ ' + k). html (v);
- });
- }
- }
- }
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
- Errorplacement: function (Error, Element)
- {
- Error.appendto (Element.parent ());
- }