Database Search Solution
(New Version) Search Control
Free ASP.NET Controls
ASP.NET Media Player Control
Flash Video Player Control
ASP.NET Telecommute Jobs
Free IP Location Lookup
Test .Net Regular Expressions
CSS/Table/DIV Page Layouts
Article Sites Master List
.NET Windows Forms
General .NET Framework
Accepting Credit Cards
Validating User Input In ASP.NET 2.0 Web Applications
What you are going to learn in this tutorial?
What is Validation?
Developing robust applications is the target of every serious developer. Being sure from the correctness of the input coming to your application is one of the major practices used to rise application robustness. In this tutorial we are going to give you the skills needed to build web applications that can detect and (sometimes) correct bad inputs from your users.
What you should already know?
What is controls, and their properties?
What is input validation?
In most of web applications, the user of the application provides input to be manipulated by your web application. As the user is a human being, errors can occur while this input is being done. 'Input Validation' is a programming practice in which the developer of the web application attempts to detect incorrect user inputs and make his application act accordingly.
So, what all of this is about? We all like to skip into implementation!
Although it's not typically a good practice to skip into implementation without sufficient preparation, in the particular domain of our tutorial we can do so safely. In fact this will give you a bird view picture of the subject that will make the comprehension of the rest of the tutorial easier.
Example 1 (Download!)
In the following example we will provide you with step by step instructions regarding how to force your user to enter a value inside a text box (he will not be able to continue working without entering this value). The example is a very simple calculator that calculates the square of the number (the number multiplied by itself) entered by the user. Here's the steps you need to perform:
In the click event of our button type the following code:
Run your application and click the button
Why user input validation is important?
Any one of us once involved in developing a serious web application, knows very well that there are many reasons his application can fail. While writing the application, the scenarios of failure arise one by one and are to be handled by the developer so that his application is robust enough. If you asked any experienced developer about what most reduces these scenarios of failure, he will typically answer: "If just I'm sure the value of x is restricted to ..." Being sure from the characteristics of user input does not only reduce these scenarios of failure to a great extent, but also saves you from writing code to handle many cases of input because you are sure that these cases will never arise. It's 'Input Validation' what makes you that sure and lets you focus on the core of your application instead of wasting your time handling every case of input one by one.
Now, relax and let's start from scratch!
Thanks to ASP.NET 2.0, where you can find several controls that can be used to achieve the user input validation scheme we are talking about. Examples of these controls are: CompareValidator, CustomValidator, RangeValidator, RegularExpressionValidator, RequiredFieldValidator, and ValidationSummary. In this tutorial we will provide you with step by step instructions regarding how to utilize these control to benefit from the validation features we mentioned before.
Can you validate everything?
Yes! only if you know exactly what will be the user input. Just kidding! No one (of course) can know exactly what will be the user input and if that happened then there is no need to make the user provide this input from the first place. This is the case, you cannot validate all types of user inputs.. Examples of inputs that cannot be validated are inputs that has no expected pattern (Example: An HtmlTextArea in which the user writes his opinion about your web site!). Although you can force the user to input a minimum number of characters in this example, you cannot (at least using the validation controls we present in this tutorial) check whether his input is meaningful or not.
How to think?
First of all, you have to decide what input you are going to validate. Please be more specific and ask yourself, what control and what property of that particular control I need to validate? To help you decide, here's a list of typical controls along with the typically validated property of this control.
Below we list some validation controls along with the purpose(s) in which each of them can be used:
So, how to use the validation controls in general?
As you may noted in Example 1.1, the general approach is to draw a validation control and associate it with the control to be validated via the validation control's ControlToValidate property.
Example 2 (Download!)
Here we need the user to enter a value from 0 to 100 and we will show the associated grade, where 0 to 49 is associated with 'Poor', 50 to 70 is associated with 'Good', 71 to 90 is associated with 'Very Good', and 91 to 100 is associated with 'Excellent'. We will need to make sure that the user had entered a value and to ensure that this value is between 0 and 100.
In the click event of our button type the following code:
Run the example and note that it does not accept values outside 0 ... 100
You may note that we set some properties that are specific to the validation control we used (like: MinimumValue, MaximumValue, Type). Of course different properties need to be used as we opt to use a different validation control)
For further information
Refer to the online copy of Microsoft Developers Network at http://msdn.microsoft.com or use you own local copy of MSDN.
Tutorial toolbar: Tell A Friend | Add to favorites | Feedback |