admin管理员组文章数量:1135599
I'm using the jQuery validation plugin to validate a form, and I'd like to remove the validation and submit the form if a certain link is clicked.
I am submitting form with javascript like jQuery('form#listing').submit()
, so I must remove the validation rules/function with javascript.
The problem is that I can't figure out how to do this. I've tried things like jQuery('form#listing').validate({});
and jQuery('form#listing').validate = null
, but with no luck.
I'm using the jQuery validation plugin to validate a form, and I'd like to remove the validation and submit the form if a certain link is clicked.
I am submitting form with javascript like jQuery('form#listing').submit()
, so I must remove the validation rules/function with javascript.
The problem is that I can't figure out how to do this. I've tried things like jQuery('form#listing').validate({});
and jQuery('form#listing').validate = null
, but with no luck.
12 Answers
Reset to default 71Trigger the DOM submit method to skip the validation:
$("#listing")[0].submit();
You can remove events of nodes with unbind:
jQuery('form#listing').unbind('submit'); // remove all submit handlers of the form
What you are probably looking for is that the validation plugin can also unassign itself from the submit event:
jQuery('form#listing').validate({
onsubmit : false
});
For both of these you should be able to follow up with a call to .submit()
to submit the form:
jQuery('form#listing').unbind('submit').submit();
var form = $('#my_form_id').get(0);
$.removeData(form,'validator');
Is really working.
You can also use the public "rules" function in this way:
$('input, select, textarea').each(function() {
$(this).rules('remove');
});
That's what I'm using ;)
You can add a css class of cancel to an element (button, input) so that it skips the validation
I have found none of the other ways helpful if you wanted to toggle on/off your forms validation (e.g. different validation for payment types...)
What I have had to do (this is not an nice fix but it does work)
Set up your validation:
jQuery('#form').validate();
Disabling the form:
jQuery('#form').validate().currentForm = '';
Enabling the form again:
jQuery('#form').validate().currentForm = jQuery('#form')[0];
This seems to work for me now:
var form = $('#my_form_id').get(0);
$(form).removeData('validate');
$("#formName").validate().settings.ignore = "*";
Refer : https://github.com/jzaefferer/jquery-validation/issues/725
For all the tags input of form:
$('#myform').validate().settings.ignore = '.valid';
$('input').addClass('valid');
It works for me.
I recently upgraded to 1.5.5 of Jörn's validator script and the removeValidator function has been deprecated.
...
jQuery.extend(
jQuery.fn,
{
removeValidator: function(){
this.unbind();
jQuery.removeData(this[0], 'validator');
}
...
in 2022
the answers above do not work now for me,
So I've written this js method, this would remove the jquery validation correctly
function RemoveJQVRule(rulename, inputname) {
$(`[name="${inputname}"]`).rules('remove', rulename);
$(`[name="${inputname}"]`).removeAttr(`data-val-${rulename}`);
// message span element
$(`#${inputname.replace(/\./img, '_')}-error`).html("");
$(`[data-valmsg-for="${inputname}"]`).html("");
}
Examples:
RemoveJQVRule('required', 'Shipper.Contact.NationalId');
RemoveJQVRule('maxlength-max', 'SomeInputName');
RemoveJQVRule('regex', 'SomeInputName');
Note:- if it does not work you may need to edit it a little bit (the css selectors)
Thanks
The validation lib now provides a method to completely remove the configuration from the form.
Official example:
/*
* On SPA page start.
*/
var validator = $( "#myform" ).validate();
/*
* Just before SPA page's navigation away.
*/
validator.destroy();
/*
* After this point the #myForm form is back to its original boring state.
*/
本文标签: javascriptHow do I remove jQuery validation from a formStack Overflow
版权声明:本文标题:javascript - How do I remove jQuery validation from a form? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736929984a1956749.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论