Using jQuery Validate , there is a way to determine if a form is valid without returning the form.
In this question, how to add submitHandler function when using jQuery Unobtrusive Validation? I am listening to the invalid-form.validate event to show the user a user error when the form has been validated and has errors.
In this question about how to prevent double clicking on submit , I will disable the submit button after submitting the form, but only if the form is valid, so the user can take corrective actions and resubmit after cleaning the form.
The problem is now invalid-form.validate fires twice.
Since the invalid-form.validate fires before the form submission handler, I already know the number of errors. The .valid() method saves the number of errors somewhere that I can check against this value instead of re-checking the form.
Here is an example in stack fragments. Before sending the required fields, click "Submit" and you will see two error messages.
$("form").validate();
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.13.1/jquery.validate.js"></script> <form > <input type="text" name="Name" required /><br/> <input type="text" name="Email" required /><br/> <input type="submit" value="Submit"/> </form>
I could save them in an element in my invalid-form.validate , but this will not be updated when the form is valid, and also strongly combines my double-click code with my error code. If I select the user error code later, I will have to rewrite the double-click warning.
Any ideas?
Kylemit
source share