Form Validation

It's been said that the only way to write a perfect data input program is to eliminate the users. While you probably don't want to go that far, you should recognize that the single greatest source of bad data is bad programs. The best data entry programs don't let users enter invalid data. Less elegant but still acceptable programs let users know when they do enter invalid data, but neither type accepts invalid data. In contrast, bad programs not only let users enter invalid data, they also skip the validation, invariably resulting in data integrity problems, expensive fixes, angry clients, and a lot of trouble all around.

To solve the problem, you must perform data validity checks. The general rule to follow is that the closer you perform these checks to the point of input, the more likely you are to get consistently good data. By checking the data at the point of input, you eliminate the need to check it during future operations, which both eliminates potential errors and provide an improved user experience. Unfortunately, I've seen programs that go far overboard by displaying annoying messages whenever a user types a single invalid key—something that even the best typists do occasionally. Similarly, many programs check field validity only after the user completes the entire form. In certain cases, that's acceptable, but more often, a better method is to check the fields when the user completes a defined part of the input task.

For example, on the login Web Form, you know that both the User Name and Password fields must contain a value, because you will be unable to authenticate the user without both values. Therefore you must ensure that both values have been entered. Beyond that, you should have rules for the contents of each field. For example, you might force passwords to be at least six characters, and you might not allow the username to contain any characters except letters and numbers.

Whenever a user submits the form without meeting the validation requirements, you want to redisplay the form and show a message stating what the problem is and how to fix it. In ASP.NET, you can perform many common data validation tasks without writing any code, using a set of server controls called validators.

Was this article helpful?

0 0

Post a comment