Can a html5 form validation
Yes, use the constraint validation API. See http://www.w3.org/TR/html5/forms.html#the-constraint-validation-api. For instance, you can call element . checkValidity()
. See also https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5/Constraint_validation or https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/checkValidity. For example:
var email = document.getElementById('email');
var valid = email . checkValidity();
if (!valid) ...
or use the invalid
event which is fired when checkValidity()
fails:
email.addEventListener("invalid", function() { alert("Email invalid"); });
You can use setCustomValidity
to set the validity status and/or error message:
if (!email.value) email.setCustomValidity("Email is missing");