Form not getting jQuery validation

I had a small problem when using the jQuery validation plugin in my .net / MVC projects: Validation usually works fine, the problem occurs during submission: I want the form not to be submitted if it has any validation errors , and for this I installed debug: true;

But after setting it to true, when the submit button stops responding, even if everything is in order, that is, no verification errors, nothing happens, just by pressing the submit button. And if you remove debug: true, it will submit the form, even if it has validation errors. I want to solve this problem, the middle way, that is: When you click the "Submit" button, if there are errors, it does not send the form and shows errors And if there are no errors, then simply sends them.

Help plz .. Thanks!

<script type="text/javascript"> $(document).ready(function() { $("#productSubmit").validate( { debug : true, rules : { prdctttle : { maxlength : 50, required : true, onlyChars : true } , prdctdscrptn : { maxlength : 250, required : true, onlyChars : true } } , messages : { } } ); } ); $.validator.addMethod('onlyChars', function (value) { return /^[a-zA-Z ,-!]+$/.test(value); }, 'Please enter a valid name with only alphabets'); $("#productSubmit").validate( { submitHandler : function(form) { if ($(form).valid()) form.submit(); return false; // prevent normal form posting } } ); </script> 
+8
javascript jquery jquery-validate forms asp.net-mvc-3
source share
2 answers

Change your last line to check if the form is valid before submitting:

 $("#productSubmit").validate({ submitHandler: function(form) { if ($(form).valid()) form.submit(); return false; // prevent normal form posting } }); 

EDIT: I forgot to add "return false"; to stop submitting the form in normal mode (this only happens when the check passes).

+11
source share

GregL's answer is for the most part correct.

What ultimately got this work for me was also an addition:

 onsubmit: false 

End Code:

 $("#productSubmit").validate({ onsubmit: false, submitHandler: function(form) { if ($(form).valid()) { form.submit(); } return false; // prevent normal form posting } }); 

This overrides the default submit behavior and handles the final check and submit check in submitHandler.

+2
source share

All Articles