Validation
Form validation is of vital importance to a website’s security as well as its usability. The validation process evaluates whether the input value is in the correct format before submitting it. For example, if we have an input field for an email address, the value must certainly contain a valid email address; it should start with a letter or a number, followed by the @ symbol, then end with a domain name.
The HTML5 specification has made validation that bit easier with the introduction of new input types such as
email, url, and tel, and these also come packaged up with predefined validation. Whenever the value given is not met with the expected formatting, these input types will throw an error message thus preventing submission.![]() |
| Invalid email address error message (Chrome) |
Using the Pattern Attribute
The pattern attribute is only applicable on the input element. It allows us to define our own rule to validate the input value using Regular Expressions (RegEx). Again, if the value does not match the specified pattern, the input will throw an error.
For example, say we have a username input in our form. There isn’t a standard type for username, hence we use the regular text input type:
- <form action="somefile.php">
- <input type="text" name="username" placeholder="Username">
- </form>
Add [a-z]{1,15} as the value of the pattern attribute in our username input:
- <form action="somefile.php">
- <input type="text" name="username" placeholder="Username" pattern="[a-z]{1,15}">
- </form>
![]() |
| The error message, stating the pattern is not matched |
Customizing the Validation Message
Fortunately, we can customize the message to be more helpful, and we have a couple of ways to do so. The easiest approach is to specify a title attribute within our input element:
- <form action="somefile.php">
- <input
- type="text"
- name="username"
- placeholder="Username"
- pattern="[a-z]{1,15}"
- title="Username should only contain lowercase letters. e.g. john">
- </form>
![]() |
The second approach will solve this for us.
Replacing the Default Validation Message
Let’s now replace the default “Please match the requested format” with a completely customized message. We’ll use a bit of JavaScript to do this.
Begin by adding an id to the input element, so as to be able to select it conveniently.
- <form action="somefile.php">
- <input
- type="text"
- name="username"
- placeholder="Username"
- pattern="[a-z]{1,15}"
- id="username">
- </form>
- var input = document.getElementById('username');
- input.oninvalid = function(event) {
- event.target.setCustomValidity('Username should only contain lowercase letters. e.g. john');
- }
We should now find the custom message seamlessly replacing the default.
![]() |
If you found this post interesting, follow and support us.
Suggest for you:
Creating an MP3 Player with HTML5
Learn Web Development Using HTML5 Advanced Programing
Ultimate HTML5,CSS3 & JAVASCRIPT To Create Your Own Interractive Websites
Easily Learn HTML 5 From Scratch
Ionic by Example: Create Mobile Apps in HTML5




No comments:
Post a Comment