So I have a form, but I don't need to be submitting the information to the server just yet... What I need, is to just run the fields through the HTML5 built-in validation conditions (such as email, etc.), and if true, just execute a specific function...
So far, I've come up with this...
function checkform()
{
var /* all the elements in the form here */
if (inputElement.validity.valid == 'false')
{
/* Submit the form,
this will cause a validation error,
and HTML5 will save the day... */
} else
{
navigateNextStep();
}
}
That's the logic I've come up with so far, and its a little backhanded because I'm submitting KNOWING that there's an invalid value, hence triggering the validation prompts...
My only problem with the above logic, is that I have about, 7-8 input elements, and I find the option of doing the following rather, 'dirty':
var inputs = document.getElementsByTagName("INPUT");
if (!inputs[0].validity.valid && !inputs[1].validity.valid && ...)
Ideas?
checks html string tag by tag if valid. trys to render html string. compares theoretically to be created tag count with actually rendered html dom tag count. if checked 'strict', <br/> and empty attribute normalizations ="" are not ignored.
setCustomValidity() In an input element, it is used to set the validationMessage property. It is really very easy to control a custom validation message in an HTML5 form. Let us find out how.
Validation-related attributesThe value must be greater than or equal to the value. There must be a value (if set). Unless the step is set to the any literal, the value must be min + an integral multiple of the step. The number of characters (code points) must not be less than the value of the attribute, if non-empty.
You can just call formEl.checkValidity()
... This will return a boolean indicating whether or not the whole form is valid, and throw appropriate invalid
events otherwise.
The spec
A brief JSFiddle to play with
I'm not sure how you're expecting submitting the form to trigger the browser's validation UI, though. Calling formEl.submit()
seems to result in a submission regardless of the validation state. This is noted at the bottom of The H5F project page, which says:
Safari 5, Chrome 4-6 and Opera 9.6+ all block form submission until all form control validation constraints are met.
Opera 9.6+ upon form submission will focus to the first invalid field and bring up a UI block indicating that it is invalid.
Safari 5.0.1, and Chrome 7 have removed form submission blocking if a form is invalid, most likely due to legacy forms now not submitting on older sites.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With