:valid, :invalid, and :required CSS Pseudo Classes
Let's be honest, form validation with JavaScript can be a real bitch. On a real basic level, however, it's not that bad. HTML5 has jumped in to some extent, providing a few attributes to allow us to mark fields as required or only valid if matching a given regular expression. What some people don't know is that you can style elements base on their required, valid, or invalid values. Here's how!
The CSS
Each state is colon-separated from the element it's associated with:
/* basics */
input:required {
border: 1px solid blue;
}
input:valid {
border: 1px solid green;
}
input:invalid {
border: 1px solid red;
}
These pseudo classes are straight forward and useful. Here we're changing only borders, but you could use :before
and :after
to place text or an image next to each field, representing their state.
Being able to style elements based on invalid or valid information is something we've shimmed forever with JavaScript, but now we can do so (to some degree) with pure CSS!
![From Webcam to Animated GIF: the Secret Behind chat.meatspac.es!]()
My team mate Edna Piranha is not only an awesome hacker; she's also a fantastic philosopher! Communication and online interactions is a subject that has kept her mind busy for a long time, and it has also resulted in a bunch of interesting experimental projects...
![5 Ways that CSS and JavaScript Interact That You May Not Know About]()
CSS and JavaScript: the lines seemingly get blurred by each browser release. They have always done a very different job but in the end they are both front-end technologies so they need do need to work closely. We have our .js files and our .css, but...
![jQuery Chosen Plugin]()
Without a doubt, my least favorite form element is the SELECT
element. The element is almost unstylable, looks different across platforms, has had inconsistent value access, and disaster that is the result of multiple=true
is, well, a disaster. Needless to say, whenever a developer goes...
![Control Element Outline Position with outline-offset]()
I was recently working on a project which featured tables that were keyboard navigable so obviously using cell outlining via traditional tabIndex=0
and element outlines was a big part of allowing the user navigate quickly and intelligently. Unfortunately I ran into a Firefox 3.6 bug...
A small note:
:before
and:after
only works with elements that have content.input
s doesn’t, so these pseudo-elements won’t work.Amazing post, so in your example you are testing it with input text and email address is that all ?!
“now we can do so (to some degree) with pure CSS!”
could you please go into more detail about that degree? Browser support? Is this CSS3 only?
@Sumit – I can’t find any references on caniuse.com or anything similar. From my testing it works on: latest Chrome, latest Firefox, lateset Safari and IE10+. It doesn’t work on IE9 or below.
The :before and :after pseudo-elements elements interact with other boxes… as if they were real elements inserted just inside their associated element. More… http://www.corelangs.com/css/basics/pseudo.html CSS pseudo-elements
Eric
Bug in IE usign pseudo-elements
http://codepen.io/diegoleme/pen/cJyjF
A pseudo-class is similar to a class in HTML, but it’s not specified explicitly in the markup. Some pseudo-classes are dynamic — they’re applied as a result of user interaction with the document.
for full implementation of pseudo class to refer here:
http://www.mindstick.com/blog/711/CSS%20Pseudo%20Class