This tutorial covers Validating User Input with C# covers Overview of ASP.NET Validation Controls , Using the Simple Validators , Using the Complex Validators and Summarizing Results with the Validation Summary Control.
In this tutorial we will see the validation controls. The purpose of the validation controls is to validate the user input. Asp.net provides the developer with different types of validation controls. One most important point to note is that the validation is done on the client side as well as on the server side. You can always turn the validation on the client side off using the enable client side property to false. Lets see the difference between the client side validation and the server side validation.
Client side validation
When you place validation code on the client side, validation does not require a postback operation and provides fast responses to the user. However, because the validation code is outside the Web Server, it might be possible for the client to spoof the Web Server with invalid data. The addition to this, client-side validation requires the client to be capable of running scripts. That might be an issue with old browsers and some new browsers in which users turn off script execution thinking that scripts are unsafe. This client-side validation should never be used as the only validation technique to validate data on a Web page.
Server Side Validation
When the validation code is placed the server side, the process of validation might be slow because a form might involve multiple roundtrips to the Web Server before all the data is validated. On the other hand, because the Web Server is performing all the validation, you can trust the validated data. Server-side validation works well with even primitive browsers because it does not assume any specific browser capabilities.
(MCAD/MCSD Developing and implementing Web Applications with Visual C# .NET and Visual Studio. NET)
Required field validator is used for required fields. Fields that cannot be left empty is validated using the required field validator. A good example will be a TextBox which is used for taking the username as the input. Since the user must enter the username in the TextBox in order to access the website. The required field validator can be used with the TextBox control to validate the input. If user does not enter any data in the TextBox than an error message is displayed and further processing of the request will be stopped. In order to set the Required validator on the TextBox control just drag and drop the validator on the webform and set its ControltoValidate property to the TextBox id you want to validate.
private void Button1_Click(object sender, System.EventArgs e)
{
if(Page.IsValid == true)
{
// Do the processing here
}
}
The Page.IsValid property is true if the page does not have any error messages to display. The Required field validator performs the validation first on the client side and than on the server side.
{mospagebreak title=Regular Expression Validator}
Regular Expression Validator is used to check the user input against some built in regular expressions. The control provides the RegularExpression collection property that can be used to select the desired Regular Expression. Some of the built in expressions are Email, postal code, US telephone number and many more. If the user input does not match the regular expression expected an error will occur and the request for the resources will not be authorized unless the user corrects the input data.
Apart from the regular expressions that ship with the Microsoft.net framework you can also make your own regular expressions using the using System.Text.RegularExpressions; namespace.
{mospagebreak title=Range Validator}
The Range Validator control is used to check whether the input control contains value in the specified range. You can check the range of values against different data types such as String, Date, Integer and so on.
The two most important properties of the range validator control is the Maximum value and the minimum value. The range can be used when you need to restrict the user data. Suppose you want the user to enter the number in the TextBox which is between 20 and 25 you can use range validator mimimum and maximum properties to restrict the user input.
if(Page.IsValid == true)
{
RangeValidator1.MinimumValue = 20;
RangeValidator1.MaximumValue = 25;
}
{mospagebreak title=Compare Validator}
The compare validator control is used to compare the input server control’s value. The compare validator can be used to compare against a value or another control. If both the ControlToCompare and ValueToCompare properties are set for a CompareValidator control, the ControlToCompare property takes precedence.
A good use of the compare validator is to check whether the passwords entered by the user in the two TextBoxes while registering for a website are same or not. This validator also performs validation on the client side as well as the server side. So, no postback will be required when doing the comparison and hence resources will be saved and performance will increase.
{mospagebreak title=Custom Validator}
Custom Validators can be used to make your own custom validation expressions. You can find many free regular expressions on the website www.Regexlib.com. Here is a small example of the Custom Validator Control.
// This is the server side validation
// Double click the Custom Validator and write this code
{
string strPalindrome = args.Value;
string strReverse = "";
// Reverse the string
for(int intI = strPalindrome.Length -1; intI>=0; intI--) strReverse = strReverse + strPalindrome[intI];
if(strReverse == strPalindrome)
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
This validator simply checks that if the string entered is palindrome or not. (Developing and Implementing Web Applications, Kalani).
{mospagebreak title=Validation Summary Control}
The validation summary control is used to present the user with the summary of the errors that has occurred on the page. Its a good way of displaying all the errors at one place so that user does not have trouble time finding them. Validation Summary control gives the user the feature to display the error in the form of bulleted list or numbers.
I hope you enjoyed the Tutorial. Happy programming !