Stijn writes:
Now I ran into a site a while ago that had a basic form with a gray border, if I would enter a correct contents the outline would be green, if I would enter something like: fake@fakemail as the contact mail address the outline would be red. I really like this effect but can’t seem to find a tutorial on it. Maybe you have a good solution?
I'm not sure if we can check if the email address is a real email address on the fly, but I think I get what you're asking.
As someone is typing an in an input field, it would be nice give feedback to the user as they are typing if they have satisfied that field's validation criteria. This article will explain one way of achieving this effect using JavaScript and CSS.

Go straight to the final example to see it in action, and to download a zip of the html, css, js, and images used.
Stijn's question asked about changing the border color of the text input. I'm going to describe a different feedback mechanism here (changing the fieldset) because some browsers, namely Safari, don't care much for borders on text inputs.
Start at the beginning - testing for length
Let's start with field for creating a username.
<fieldset>
<label for="username">Create a Username:</label>
<input
type="text"
id="username"
maxlength="16" />
</fieldset>
For our form, in order for the username to be valid, it should be at least 8 characters long, and no more than 16 characters. The maxlength attribute in the HTML takes care of the high limit. As for the low limit, that's where we can introduce some JavaScript.
JavaScript
In English: The function tests to see if the length of what I typed is greater than 7 characters. If so, do something. If not, do something else.
function checkUsername(whatYouTyped) {
var txt = whatYouTyped.value;
if (txt.length > 7) {
... do something to indicate job well done ...
} else {
... do something else ...
}
}
As for what to do, what if we took the containing fieldset and changed it's class to something like "welldone"?
function checkUsername(whatYouTyped) {
var fieldset = whatYouTyped.parentNode;
var txt = stringinput.value;
if (txt.length > 7) {
fieldset.className = "welldone";
} else {
fieldset.className = "";
}
}
The class can be defined in the CSS to assign a background color to the fieldset.
fieldset.welldone {
background:green;
}
We need to check for this validation on every keystroke, so we attach it to the HTML using onkeyup, like so:
<fieldset>
<label for="username">Create a Username:</label>
<input
type="text"
id="username"
maxlength="16"
onkeyup="checkUsername(this);" />
</fieldset>
View Example 1 - checking for input length.
A step further to indicate "almost there".
We'll also have people create a password. We're a good company, so we let them create a password with as little as 4 characters. But we also feel a sense of responsibility to encourage (not impose upon) people to use longer passwords, at least 8 characters, as a security measure.
The HTML for the password field will look like this:
<fieldset>
<label for="password">Create a password:</label>
<input
type="password"
id="password"
onkeyup="checkPassword(this);" />
</fieldset>
The JavaScript will be modified to serve an additional outcome based on length.
- If the password is at least 4 characters long, that's good enough to continue.
- If the password is at least 8 characters long, that's even better, because it's oh-so-very secure.
function checkPassword(whatYouTyped) {
var fieldset = whatYouTyped.parentNode;
var txt = whatYouTyped.value;
if (txt.length > 4 && txt.length < 9) {
fieldset.className = "almostthere";
}
else if (txt.length > 8) {
fieldset.className = "welldone";
}
else {
fieldset.className = "";
}
}
View Example 2: checking for various input lengths.
Validating against regular expressions
If your password required letters AND numbers AND special characters, things get a little more complicated. You can't just test for length anymore, you have to match the input to a certain pattern.
Same for email addresses, which follow a certain pattern: blah@blah.blah.
Now I'm no regular expression expert. The one I'm about to use in the next example is not necessarily one I put my stamp of approval on. Basically, I did a quick google search and pulled up the first regular expressions that worked to get the point across.
Here's the HTML for the email input:
<fieldset>
<label for="email">Enter your email address:</label>
<input
type="text"
id="email"
onkeyup="checkEmail(this);" />
</fieldset>
And here's the JavaScript, which matches the input text to a regular expression, and changes our fieldset class accordingly.
function checkEmail(whatYouTyped) {
var fieldset = whatYouTyped.parentNode;
var txt = whatYouTyped.value;
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(txt)) {
fieldset.className = "welldone";
} else {
fieldset.className = "";
}
}
View Example 3: testing against a regular expression.
Make it prettier
We've shown some butt ugly validation hints so far - it'd be nice to do something a little prettier. Remember Form Field Hints? Here's an example to show how the validation hint concept can build nicely upon the Form Field Hints concept.
View Final Example: Validation Hints with Form Field Hints
Comments (24)
Alas, it'd fail on my perfectly-legit primary email account (I suppose we'll see if it works in the comment field). '+' *is* valid in the username side of things, though not many form designers seem to believe it.
Posted May 11, 2007 3:22 PM | #
@Karen:
So it does. I blame the regular expression, which I just borrowed from some other source and I don't endorse.
I realize that very few designers will read the part of the article that says "i don't endorse this regular expression", so if there are any regular expression experts out there who can offer one to use for email (and note - it should allow a "+" sign for that first part of the pattern), please share and I'll include that with the zip.
Posted May 11, 2007 3:34 PM | #
Me likes very much some cool CSS!
Thanks for the great post!
Posted May 12, 2007 7:32 AM | #
help me fix this up
Posted May 14, 2007 8:53 PM | #
Yeah, unfortunately it seems like everybody has copied the same regex (and not just in the Javascript sometimes).
Unfortunately, RFCx822 is darn permissive when it comes to email addresses, and there is no simple regex for the user side. Better to ask for a confirmation version of the address and just trust that the user knows what their address is (minus any obviously SQL-injectish things). Even in Perl, arguably the most powerful regexes you can get, it's an ugly thing... go look at the source here and scroll down to the $RFC822PAT bit. Just be prepared to run screaming from the room.
http://search.cpan.org/src/RJBS/Email-Valid-0.179/lib/Email/Valid.pm
Posted May 14, 2007 9:24 PM | #
Thanks guy for this great post!
I am developing forms for my company and this post has been very helpful.
Posted May 18, 2007 12:54 PM | #
Very nice! Also when you have two fields that must contain the same value (such as a confirmation field for changing your password) it's good to show the user when both fields match.
Posted May 23, 2007 12:13 AM | #
Nicely done!
Posted May 31, 2007 10:31 AM | #
There is a small "bug" - when the text in the hint is only one line, the background image is not displayed fully. A min-height of say 30px will do it.
Posted June 21, 2007 11:09 AM | #
/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/
Posted June 25, 2007 1:02 PM | #
Hello,
This site is really nice. But, i love this code, and i think this tip can be used on professionnal website.
What's the license of the code?
I have try to find a license, or restriction on the wevsite.. without response... anyone have idea?
Thank, Patrice Grimard
Posted July 14, 2007 9:55 PM | #
Patrice,
You are free to use the code however you wish.
Posted July 24, 2007 2:38 PM | #
You are free to use the code however you wish.
搬屋公司
中秋節
Shopping">http://www.recyclebag.net/>Shopping Bag
av">http://www.av-oscar.com/av-system-projector.htm>av
Tomy
containers
Warehouse
Posted July 30, 2007 4:16 AM | #
How would you apply this method to fieldsets containing radio buttons or checkboxes?
Posted August 4, 2007 3:42 AM | #
Great work as always, I am just starting off as a developer/webmaster and your articals are helping me gain some valuable ideas and skills.
You are the best.
Posted August 10, 2007 2:22 PM | #
When I quickly enter an email address into the form field I get a slow script warning.
Can you suggest any possible solutions?
Great tutorial by the way!
Posted November 5, 2007 12:12 AM | #
Very good demonstration.
But I don't think you should place each field into a fieldset. <fieldset> was created as a container for multiple fields so that related fields can be logically grouped to improve usability. Instead of using <fieldset>, you could consider <div>.
Posted November 17, 2007 7:29 AM | #
Cool :-)
Posted December 16, 2007 2:38 PM | #
Wow. Good for me.
Posted February 23, 2008 4:04 PM | #
Nice guide.
Looking at your final example, its great that once a field is completed to meet the validation requirements a green tick stays next to the field. But what about when the field isnt completed? The form just seems to accept it.
Could the code be modified to leave the blue icon (or some sort of red cross) for a field that is incomplete or doesnt meet the validation requirements?
Posted March 8, 2008 10:25 AM | #
This code did help. but for the validation (suppose name field where no digits or special characters are allowed) it doesnt work.
i mentioned in regular exp tht if the input keystroke is a digit or a special character then an alert box should pop up and should not allow the user to enter anything apart from the letters.
but, it takes up the value of a keystroke the second time i.e if i enter "1" the first time, then it allows it to be entered in the name field and when i hit another key then it shows up alert box coz of the "1" that i entered earlier.
I dont know how to get over it.
Posted March 26, 2008 5:35 AM | #
Doesn't work to me!
Posted March 28, 2008 12:31 PM | #
This Post is very good, just i am lokking
Posted April 2, 2008 7:00 PM | #
Great work as always
Posted April 6, 2008 12:12 PM | #