Input Validation With ASP.NET, Part 1
(Page 1 of 6 )
Often, application errors are caused by "bad" data entered by end-users. Find out how to protect your Web application from bad data by enforcing validation rules on user input with ASP.NET's Validator objects. This article discusses forcing user input for specific fields, constraining input to a specified range of values and ensuring that the data entered satisfies comparison tests.
Being Bad
One of the primary reasons for application errors (and the often-disastrous consequences that follow) is the "bad" data entered by the application's end-users. It's not uncommon to find applications falling apart because the user made a mistake while entering a critical piece of information, especially if the application's been written by a novice developer too inexperienced to know better.
If that novice developer is you, you're going to thank your stars that you found this article today.
Now, you can't stop a user from entering bad data into your application, but you can certainly check what's been entered and trigger off warnings if it's incorrect. This process is called input validation, and it's something you must do in every application you develop. This is twice as true for Web applications, which involve lots of forms and databases - if you don't have input validation in your Web-based application, you run the risk of corrupting your database by allowing incorrect, unverified values to be stored in it.
Over the next few pages, I'm going to show you how you can wrap your code in bulletproof Kevlar, so that no matter how bad the user input is, your code will detect it, warn the user about it, and will keep ticking, instead of blowing all its fuses. In ASP.NET, doing this is surprisingly simple - if you know about the built-in Validators.
Next: Making Friends >>
More ASP.NET Articles
More By Harish Kamath (c) Melonfire