I have this form in my app and I will submit it via AJAX, but I want to use HTML5 for client-side validation. So I want to be able to force the form validation, perhaps via jQuery.
I want to trigger the validation without submitting the form. Is it possible?
I found this solution to work for me. Just call a javascript function like this:
action="javascript:myFunction();"
Then you have the html5 validation... really simple :-)
Here is a more general way that is a bit cleaner:
Create your form like this (can be a dummy form that does nothing):
Bind all forms that you dont really want to submit:
Now lets say you have an
<a>
(within the<form>
) that on click you want to validate the form:Few notes here:
checkValidity()
is enough (at least in chrome). If others could add comments with testing this theory on other browsers I'll update this answer.<form>
. This was just a clean and flexible way to have a general purpose solution..This way works well for me:
Add
onSubmit
attribute in yourform
, don't forget to includereturn
in the value.Define the function.
not a best answer but works for me.
You don't need jQuery to achieve this. In your form add:
or in JavaScript:
In your
buttonSubmit
function (or whatver you call it), you can submit the form using AJAX.buttonSubmit
will only get called if your form is validated in HTML5.In case this helps anyone, here is my
buttonSubmit
function:Some of my forms contain multiple submit buttons, hence this line
if (submitvalue == e.elements[i].value)
. I set the value ofsubmitvalue
using a click event.