I'm using HTML5 for validating fields. I'm submitting the form using JavaScript on a button click. But the HTML5 validation doesn't work. It works only when then input type is submit
. Can we do anything other than using JavaScript validation or changing the type to submit
?
This is the HTML code:
<input type="text" id="example" name="example" value="" required>
<button type="button" onclick="submitform()" id="save">Save</button>
I'm submitting the form in the function submitform()
.
Try with
<button type="submit">
you can perform the functionality ofsubmitform()
by doing<form ....... onsubmit="submitform()">
You should use form tag enclosing your inputs. And input type submit.
This works.
Since HTML5 Validation works only with submit button you have to keep it there. You can avoid the form submission though when valid by preventing the default action by writing event handler for form.
This will give your validation when invalid and will not submit form when valid.