By Peter Bell

Form Validation: On-blur or On Submit?

I've noticed recently a trend towards validating fields when you leave the field, so the form will immediately let you know whether your email address was valid or your username is unique. Obviously some dependent validations can't be done in this way, but in general terms it seems like a pretty good default approach to form validation.

What do you think? Should validations be performed ASAP or should they wait for a form submission? Obviously it depends - but on what? What would move you towards including or avoiding on blur field validations?

Related Blog Entries

Comments
I think it depends on how the validation error appears. If it's non-obtrusive - for instance, using the Spry validation in Dreamweaver CS3 that uses CSS to write messages to the right of the field - then I have no problem with onBlur validation. However, if you're going to use a JavaScript alert box, then there are few things I find more annoying than onBlur. There are times when I *know* the field isn't valid yet, but I want to go ahead and finish the rest of the form first. If I have to click an annoying dialog box to get out of the field, that will piss me off. But if it's just a line of text or maybe a color change, then whatever, it's fine.

Of course, neither of these should be used to the exclusion of good server-side validation, but that's a different discussion...
# Posted By Rob Huddleston | 9/7/07 11:52 AM
Personally, I don't like anything that interrupts my flow. Messages appearing with no apparent trigger tend to distract me.

For short forms, at least, I prefer the show-me-everything-at once approach (onsubmit). For longer forms, a more incremental approach (onblur) may be better in order to prevent the user from being overwhelmed if there are a lot of issues and to prevent error messages from appearing above or below the fold and out of sight.
# Posted By Rob Wilkerson | 9/7/07 11:54 AM
The problem is that doing onBlur validation only works if they actually focus on the form field. So really, anything you validate onBlue must also be validated onSubmit anyway.
# Posted By Brian Kotek | 9/7/07 11:55 AM
Hi Guys,

Good points all - I should have been more precise.

Firstly, client side validation NEVER removes the need for server side validation. Javascript could be turned off, I could use a tool to modify the form fields and values being sent - you can NEVER rely on user input.

Secondly, I'm thinking the sites where you enter something into a field and as soon as you leave that field it gives you any error message on the right of the field in red text. I personally dislike the forms that tell you the field is wrong while you are still typing in the field like Live validation: http://www.livevalidation.com/examples, but I do like the idea of the message appearing to the right of the field on tabbing to the next field as it provides really immediate feedback rather than having to wait until the form is submitted and then find the elements that were problematic.

Thoughts?
# Posted By Peter Bell | 9/7/07 12:06 PM
I think validation on blur generally provides a better user experience, as long as the error message isn't a JS alert and doesn't prevent me from tabbing to other fields and finishing my input.

Here's an example of why onblur is better: I just tried submitting this post 3 times in a row, before I realized that an email address was required. The error message is the same weight and color as the rest of the text, plus it appears at the top of the page and I was looking at the bottom at the captcha, so it took 3 tries before I saw it. If a message had appeared as I tabbed out of the empty email box, I never would have had this problem :)
# Posted By Seth Petry-Johnson | 9/7/07 12:52 PM
Cool example :->
# Posted By Peter Bell | 9/7/07 12:56 PM
I follow the "prevent, don't scold" philosophy.

Don't let the user click submit and then yell at them that their form is invalid. Rather, guide them through the process while they're filling out the fields, and don't enable the submit button until the form is good to go.

For Flex 2, see this quickstart: http://www.adobe.com/devnet/flex/quickstart/valida...
# Posted By darron | 9/7/07 1:07 PM
@Darron:

There's something to be said for that, but you have to let people know why the submit button isn't enabled. I assume that's what you mean by "guide them through the process", but what's your strategy for providing that guidance?
# Posted By Rob Wilkerson | 9/7/07 1:14 PM
@Darron, Thanks for the link - interesting to see the flex API for describing validations . . .

I think I'd probably keep submit enabled as otherwise users might be confused, but it's interesting to see the various options for handling this class of problem!
# Posted By Peter Bell | 9/7/07 4:01 PM
Interesting topic Peter. Glad to see more front end/UI stuff being discussed. This is really where the rubber hits the road when it comes to things like conversion rates on eCommerce sites. So much poor usability out there.

The only time I validate onBlur is in an account signup form, where the user is creating their own unique username.

OnBlur of this field, I do an ajax call to check the username they entered against the database. If it's unique, I add a little green "Username OK" below the field. If it's not unique, I add a red "username already taken, please try again" message, and refocus the username field.

I guess my thinking is that it's pretty likely that their username might already be taken, and this saves them submitting the whole form three or four times while they're trying to come up with a username that's unique.

Of course server side check is in place in case they don't have JS enabled.
# Posted By Josh Nathanson | 9/7/07 5:58 PM
Hi Josh,

Agreed. I'd hoped to be able to leave this to my design partners, but they're busy also and don't have the time to research and implement best practices, so here we go!

I agree 100% on the username check, but was also considering passwords matching, maybe even one of those password strength meters, things like that.

Also if someone is tabbing through a form I figure it wouldn't hurt to on blur to check for required fields or any other things like invalid date formats, although I get that if they don't click on the fields you won't get the on blur, so you ALSO need to do an on submit as well as the server side validations.
# Posted By Peter Bell | 9/7/07 7:51 PM
You could always try the onchange event. That fires regardless of whether you clicked in a field as long as a change was made before the field lost focus. I didn't realize that onblur didn't fire unless you *clicked* in a field.
# Posted By Rob Wilkerson | 9/7/07 10:09 PM
Oh sorry - that's me using imprecise language. believe on blur fires when a given field that HAS focus LOSES focus. However, if instead of clicking or tabbing through an entire form I just fill out the first field and then I click directly into the third field, the on blur for the second field won't fire off because I never actually went into the field at all.

At least that's my understanding . . .
# Posted By Peter Bell | 9/7/07 10:14 PM
I having been toying around with using on blur and on submit validation. I actually have a single validation routine for a given form which creates validation messages in a javascript object keyed on the field name. When the onblur event runs it updates the css of the offending field and shows the error message as a balloon tip. The validation sets the level of the error to info, error, or warning and there is a corresponding css class for each level. This way I can highlight the offending field any way I choose with css.

With this method It's pretty obvious while entering the form where the problems are before the user even hits the submit button. What's neat is that I can use the same validations on the submit to determine if the form is valid and what level (formValidation.getLevel()). I give the user the option to submit anyway if the level is set to warning. If the level is error I show the first error in an evil javascript Alert dialog and then jump to that field when the user hits ok.

To go a step further. I always like to have validation on the server side too. For this purpose I am keeping all validations for a form in a single xml file with both cf and js versions. I still have to keep both versions up to date, but at least they are in the same file. I have a cfc to generate the javascript and cfc file that do the corresponding validations. I am working on setting up automated testing of the rules so I can cross validate my validations, but thats not nearly ready yet.

I could say a lot more, but this is getting to be a long post. . .
# Posted By LeeH | 9/9/07 10:07 PM
@Lee, Sounds like exactly where I'm looking to go with a single authoritative validation resource used to generate the CF and the onblur and onsubmit JS validations. Please let me know if you ever post any more information about this or code samples as I'd certainly love to see what you'd done!
# Posted By Peter Bell | 9/10/07 11:14 AM
I've implemented blur functionality before and usually end up removing it, especially for heavy data entry type applications. Usually my users don't want validation on blur.

For instance, you may be entering information from paper into a web system and you want to fill in the information you have and then go back and enter the missing info. If you are tabing through the fields blur validation will get old really quick.

I guess it depends on your users and the use of the app.
# Posted By Zach | 9/17/07 3:12 PM
I guess the interface matter too. I actually don't mind seeing down the right hand side either a green tick or a red x after I tab out of each field so I can easily tell what I haven't entered correctly, but I'm guessing there's a matter of preference too.
# Posted By Peter Bell | 9/17/07 3:23 PM
My thoughts...

Displaying an indicator next to a field / highlighint the field to show if it is valid / invalid is much much friendlier than displaying all messages at the top of the screen

Validation on blur is preferable to validation soley on submit - its normally much easier to fix something when you have just entered it, rather than after completing the rest of the form

Validation on blur should be suplemented with validation on submit - which should prevent the form from being submitted until it is valid - this gets round the issue of onblur not catching any fields which have not recieved focus.

This should go without saying, but all client side validation must be enforced with server side validation - the client side validation is primarily to improve the user experience.

We have recently finished an app where we are code generating the majority of the forms - and we are also generating spry validation for the majority of fields. This vastly improves the user experience, and removes what is often a drudge for developers, in writing the same validation code over and over again - but I guess I don't need to tell you that Peter!
Hopefully we will release the Illudium templates - once we have had time to make them suitable for public consumption...
# Posted By Dan Lancelot | 10/7/07 2:29 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.005.