:valid, :invalid, and :required CSS Pseudo Classes

By  on  

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!

Recent Features

  • By
    Being a Dev Dad

    I get asked loads of questions every day but I'm always surprised that they're rarely questions about code or even tech -- many of the questions I get are more about non-dev stuff like what my office is like, what software I use, and oftentimes...

  • By
    Conquering Impostor Syndrome

    Two years ago I documented my struggles with Imposter Syndrome and the response was immense.  I received messages of support and commiseration from new web developers, veteran engineers, and even persons of all experience levels in other professions.  I've even caught myself reading the post...

Incredible Demos

  • By
    MooTools OpenLinks Class – Updated

    A long time back I coded a MooTools class called OpenLinks. The class is quite useful but the code...sucks. I've gotten much better with MooTools over the past years so I thought I'd go back and update the class to be better, faster...

  • By
    MooTools ASCII Art

    I didn't realize that I truly was a nerd until I could admit to myself that ASCII art was better than the pieces Picasso, Monet, or Van Gogh could create.  ASCII art is unmatched in its beauty, simplicity, and ... OK, well, I'm being ridiculous;  ASCII...

Discussion

  1. A small note: :before and :after only works with elements that have content. inputs doesn’t, so these pseudo-elements won’t work.

  2. Amazing post, so in your example you are testing it with input text and email address is that all ?!

  3. “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?

  4. @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.

  5. 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

  6. Diego Leme

    Bug in IE usign pseudo-elements
    http://codepen.io/diegoleme/pen/cJyjF

  7. 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

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!