Solution to the failure to submit jQuery Validation dynamic forms

Source: Internet
Author: User

Today, I am going to introduce you to a solution that cannot be submitted by jQuery Validation dynamic forms. I hope this tutorial will be helpful to you.

When jQuery Validation Plugin is used to verify the form, if Javascript is used to dynamically add an input or textarea form item to the form, it is found that the form cannot be submitted, and the newly added form item cannot pass verification, script Error.

The Form Verification plug-in version used is jQuery Validation Plugin 1.11.1.

The solution is to reset the verification rules if the content of the form changes. Example:

The Code is as follows: Copy code

$ ("# Formid"). change (function (){
Var $ form = $ ("#" + formId );
Var validSetting = $ form. validate ({
ErrorElement: 'span ',
ErrorClass: 'help-inline ',
FocusInvalid: false,
Ignore: ": hidden ",
Rules :{
Email :{
Required: true,
Email: true
}
},
ErrorPlacement: function (error, element) {// render error placement for each input type
Error. insertAfter (element); // for other inputs, just perform default behavoir
},
InvalidHandler: function (event, validator) {// display error alert on form submit

},
Highlight: function (element ){
$ (Element). closest ('. help-inline'). removeClass (' OK '); // display OK icon
$ (Element). closest ('. control-group'). removeClass ('success'). addClass ('error'); // set error class to the control group
},
Unhighlight: function (element ){
$ (Element). closest ('. control-group'). removeClass ('error'); // set error class to the control group
},
Success: function (label ){
Label. addClass ('valid '). addClass ('help-inline OK '). closest ('. control-group '). removeClass ('error '). addClass ('success ');
}
});
If (! $ Form. valid ()){
Return false;
}
$ Form. change (function () {// reset the verification rule when the form element changes
ValidSetting. rules = rules;
});

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.