I want to check validity of form on clicking the submit button. In case it is valid the form should submit otherwise do nothing.
I’ve tried disabling the submit button and on click checking validity and enabling it.
But I see two issues with this approach.
1: Clicks are not detected on a disabled button
2: Even if I manage to get a click detected, I’ll probably end up in an infinite loop. The second click after the button is enabled will again trigger the validity check and so in.
I want to check validity of form on clicking the submit button. In case
Share
You should use
event.preventDefault(). Read this: http://api.jquery.com/event.preventDefault/Rough sample code draft:
Where
validate()is your validation function, returning false in case of validation fail.Edit:
Sample handler is bind to
clickevent, because you wantIt’s better practice to use
.submit()instead of.click(), for validation to be made for any type of form submitting.Our sample stays almost unchanged: