28
HTML5 Form Field Validation with CSS3
HTML5 Built-in Form Validation
HTML5 specifications come with a full of goodness that make web devs’ lives easier.
The one of the goodies, the client form validation is the one I like a lot, because, for example, by adding required attribute to an input, I don’t need to write any additional JavaScript to warn a user, when the user submits a form without filling out the required fields. The interactive warning UI comes with browsers.

The “Speech bubble” UI is built in with HTML5-enabled browsers. (This screenshot is taken on Chrome. Other browsers like Firefox and Opera have their own interface.)
Pattern Matching
You can also validate against patterns that defined by you. The pattern attribute specifies a regular expression against the control’s value.
<input type="text" pattern="[0-9]{13,16}" title="A credit card number" />
A user is required to enter values to match a regular expression pattern, in this case, a 13 to 16 digit number.
CSS3 User Interface Selectors
There are numerous user interface state pseudo-classes. You’ve probably already known :hover, :active etc. According to this W3C Candidate Doc, there are additional pseudo-classes defined, such as :valid, invalid, in-range, out-of-range, required, optional, read-only and read-write.
So I played a bit with :valid and :invalid to check against the regular expression pattern.

The diagram shows: 1) The entry is displayed in red when the entered text is not matched.
2) The built-in HTML5 message is displayed when the user submits the form while leaving the invalid entry. No additional CSS is used.
3) The entry is in green when it matches with the defined pattern. Also, a check mark is used as an indicator to tell it is valid.
The simplified code is below. Also you can view the entire code and the working demo on jsFiddle.
<input id="cc" type="text" pattern="[0-9]{13,16}" required />
<div class="input-validation"></div>
<input id="payButton" type="submit" value="Pay Now" />
input[type="text"]:valid {
color: green;
}
input[type="text"]:valid ~ .input-validation::before {
content: "✓";
color: green;
}
input[type="text"]:invalid {
color: red;
}
I was too lazy to create an image, so I just used the unicode checkmark. Actually, what I wanted to do was that inserting the unicode content after the input with this CSS- input:valid:after. However, it is not possible to add contents to the input with the CSS since input has no document tree content. Therefore, I used the extra div after the input. (So I am using the sibling selector, ~, to specify the div).
If you would make it prettier, I suggest you should place some icons as a background-image of the input, instead of just dumping some unicode char!
input[type="text"]:valid {
background: url(thumb-up.png) no-repeat top right;
}
References
HTML5 Form Validation on SUMO (Mozilla Blog)
CSS3 Basic User Interface Module by W3C
The pattern attribute by WHATWG
27
HTML5 Input Event Handlers and User-Experience
TL;DR
Summary: Use oninput event handler (or input event handler event type) when you register an event to the HTML5 input[type="number"], instead of onchange, onkeyup/down (although you may want to include them as the fall-back).
The Whole Story
When I participated to compete for Node.js Knock-out in the end of the summer, I have learned one thing (besides all the node.js coolness) in a bitter way, from the comments by judges: “the app UI is confusing.”
Actually, I believed that the app user-interface overall was intuitive enough, however, the one mistake I made with an HTML5 form killed the user-experience – I used a wrong event handler, something that doesn’t trigger when it needed to…
So here, I explain what I didn’t know; the newly introduced event handlers for HTML5.

The screenshot above is a number input control, the input element with a type attribute which value is “number”. It represents a precise control for setting the element’s value to a string representing a number.
Simply, you can write this in html like this:
<input type="number" placeholder="Pick a number" id="numPeople" />
You can add some more custom attributes like, required, autofocus, pattern, etc, and the number-specific attributes, min, max, and step. If you are not familiar with the html5 form input attributes, I recommend you should read New form features in HTML5 by Dev Opera.
So what I wanted to do in the particular app was that populating new fields as the user chooses a number. The simple user-flow is:
1. “How many people would you like to add?” – e.g. a user picks 5
2. n-number of new fileds are populated – e.g. 5 text fileds are displayed
3. the user fills out the text fields
4. the user submit the form

To make the flow #1 and #2 work, what I did was that I registered an event listener to the number input element, and used an event type to listen for.
OK, so which event type? – Well, I initially thought of change, however, it requires the user to unfocus (blur) the field once, to get the event captured. So, I decided to use keyup so as soon as the user finish typing a number, the event is fired and the function to display the extra fields is called.
OK, it sounds fine – well, only as long as the user only enter the number manually!
Remember, the HTML5 input field type="number" has the special user-interface, a spinner control, which allows a user to increment and decrement its value. And here is the problem – the keyup event does not get triggered when the spinner up/down arrow is clicked, at least on Chrome (which the judges were using)! So when some judges tried the app for the first time without much knowing what it does, and picked a number with the spinner control, nothing happened. The app’s UX was killed right there!
So, really, which event did I need to use?
The answer is input. I did not know about the newly introduced HTML5 oninput event handler until I found this article.
Now, the action is triggered when a user manually type a number, or when the user increment/decrement with the up/down spinner!
var numPeople = document.getElementById("numPeople");
numPeople.addEventListener("input", function(e) {
var num = numPeople.value;
...
}, false);
Please see the entire code snippet and working demo!
Also, I made another small demo here so you can see when the input, keuyp, and change are captured.
onsearch Event
Additionally, there is another HTML5 event that you probably want to know – the search. The action is invoked when a user hits the enter key or clears the query (by clicking the (x) in a search control.
I can’t find any references in WHATWG or W3C, and apparently, its upport Level is the Apple extension. I tested it work on the latest Chrome, but not on Firefox 9 or Opera 11.5.
![input-search input[type=search]](http://girliemac.com/blog/wp-content/uploads/2011/11/input-search.png)
<input type="search" name="username" placeholder="Enter a Twitter username..." results="5" id="search" />
var s = document.getElementById("search");
s.addEventListener("search", function(e) {
var q = s.value;
showTweets(q);
}, false);
...
The code snippet and demo is at jsFiddle, too!
References
whatwg.org – Web Forms 2.0
whatwg.org – oninput Event Handler
Effectively detecting user input in JavaScript
Safari HTML Reference (onsearch)






