Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Submit HTML5 Form using Javascript and validate its inputs

I'm writing form and adding html5 validation attributes to its input like "required", "autofocus". I use Javascript to submit the form using document.myForm.submit() but it doesn't validate the form against the html5 validation attributes as they aren't there.

Any Suggestions?

like image 796
Ahmed Elmorsy Avatar asked Jul 28 '11 08:07

Ahmed Elmorsy


1 Answers

It appears that triggering a click on the submit button does have the correct effect: http://jsfiddle.net/e6Hf7/.

document.myForm.submitButton.click();

and in case you don't have a submit button, add an invisible one like:

<input type="submit" style="display:none" name="submitButton">
like image 143
pimvdb Avatar answered Oct 03 '22 23:10

pimvdb