HTML5 form elements provide explicit, definitive elements for different types of inputs on web pages, with support for native in-browser validation. However, that validation has a few disadvantages over traditional client-side approaches like JQuery:
HTML5 form elements are not validated as the user moves through the form, but only when the user clicks the submit button;
Perhaps more importantly, the errors provided by the browser are blandly generic, with messages such as “Please input the requested information” providing very limited guidance to the user.
For a long time I was under the impression that the browser’s native error messages could not be altered, but that’s not true: you can easily customize your HTML5 form’s feedback by using JavaScript.
I’ve provided one method of doing so previously, using CSS3 and span elements. The technique shown here uses JavaScript to customize the browser’s own built-in validation errors, providing far more variation and power.
An HTML5 form element will undergo client-side validation if it has a required attribute. In our case, we’ll call our custom validation via a function on the form’s submit button:
- <label for=email>eMail<input type=email id=email name=email></label>
- <input type=submit onclick=validate()>
Right now, if the user pressed the submit button without completing the eMail field, the function will not be found, and browser will simply respond with “Please fill out this field”. The form deserves a more informative error message. First, we need to identify the input, via a script placed at the very end of the document:
- <script>
- var email = document.querySelector('#email');
- </script>
Rather oddly, JavaScript insists that in order to create your own custom validation message, the default message for each input must be deleted first:
- <script>
- var email = document.querySelector('#email');
- email.setCustomValidity('');
- </script>
Then, we test the validity of the input:
- <script>
- var email = document.querySelector('#email');
- email.setCustomValidity('');
- function validate() {
- if (!email.validity.valid) {
- email.setCustomValidity('This field is required');
- }
- }
- </script>
If you try to complete the form at this stage, you’ll see your own custom error message inserted after pressing submit. This is a good start, but a little broad: the error will be the same no matter what the user enters, until they supply a valid eMail address. Let’s make a distinction between a blank value and one that is incomplete by changing the function slightly:
- function validate() {
- if (!email.validity.valid) {
- if (email.value.length == 0) {
- email.setCustomValidity('This field is required');
- } else {
- email.setCustomValidity('This is not a valid eMail address');
- }
- }
- }
Now the form will respond appropriately to different input states, rather than providing a generic error message. When the eMail address is correct, no error messages will be shown and the form will be submitted.
Conclusion
While it takes a little work, it’s very easy to insert your own custom error messages in HTML5 forms. There are many possible improvements to make to this approach, but perhaps the greatest – the fact that the JavaScript changes the content of the error while leaving the presentation of the messages unchanged – must be treated in a separate article, due to the somewhat complex and arcane CSS involved.
Haha, that is actually incredibly clever.
![Prometheus: Collector's Edition (Bilingual) [Blu-ray 3D + Blu-ray + DVD + Digital Copy] Prometheus: Collector's Edition (Bilingual) [Blu-ray 3D + Blu-ray + DVD + Digital Copy]](http://ecx.images-amazon.com/images/I/5192I1rtYnL._SL160_.jpg)

