mkaz.com home photography web dev personal about

Web Forms & Data Validation

Author: Marcus Kazmierczak
Created On: September 21st, 2003
Last Modified: May 28th, 2006

Just as valid today, as three years ago when this article was originally written.

Why do some web site forms require users to input their data exactly how the programmer needs it? I'm sure you've seen: please no spaces when you enter your credit card. This is pure laziness by any web developer and it really annoys me!

How hard is it to write code to strip out the spaces? It's not, it's simple. Look here's a simple line of Perl to strip out all spaces:

$input =~ s/\s//g;

Do you want the PHP code?

$input = str_replace(' ', '', $input);

Or how about Python?

input = input.replace(' ', '');

Why would you punish your customer for typing in spaces? Especially for something like a credit card number. They are trying to buy something from your site, the entire point of your website: to take the customers money, why make it even slightly more difficult for them!?

I've seen some forms which have an elaborate system to tell the user exactly what they did wrong. It would be simpler to just parse the data after to was submitted. Now I know that there can be invalid data entered, but don't assume because the format is wrong that it is invalid.

I couldn't make a purchase one time because they wouldn't accept my phone number. I tried three different ways to enter it and the site kept telling me it was wrong. I left and they lost a sale.

Dates are another example: People write them with dashes with slashes, with 4-digit years and 2-digit days. It really is not a complicated thing to write a routine to parse these properly. In fact there are libraries in every programming language that can help you do this.

Don't be a lazy web developer, spend that extra 10-15 minutes to make the form easy to use from the customer stand point. Just think of all the millions of customers and users you will have, and how much time you will save them.