Asp .net Validation controls by azamsharp

Introduction:

Validation is important part of any web application. User’s input must always be validated before sending across different layers of the application. In this article we will take a look at the validation controls provided by Asp.net.

Required Field Validator:

Required field validate control is used on controls where user must enter some data. If the data is is not entered and the required field validator is set than it stop the processing of the page until the data is entered in that control. Let’s see how we can use Required Field Validator control.

Above is the image of the TextBox that we will validate. The validation will be fired when the submit button is clicked. First let’s add a Required field validator.

As you can see in the image above that I have set the ErrorMessage property to “This field cannot be blank” which is the message that will be displayed when the user does not input any data in the textbox. The property ControlToValidate let’s the asp.net know that which control we are validating. In this case we are validating a textbox control whose name is “txtName”. After setting these properties let’s implement the button click code that will fire the validation event.

private void Button1_Click(object sender, System.EventArgs e){
	if(Page.IsValid == true)
	{
	// Do some processing here 
	}
}

In the button click event we are checking that if the page is valid or not by using Page.IsValid property. If Page.IsValid property is true than the page will be processed further. If the Page.IsValid property is false than the Required field message will be automatically displayed on the screen.

Required field validators are not only used for the TextBox controls but can also be used for DropDownList and other controls. Let’s see how we can use Validation on the DropDownList control.

As you can see in the image above that we have a DropDownList which has several items. The first choice is “Select any Item”. You don’t want the user to submit the page with “Select any item” being selected. But you want the user to choose between Item1, Item2 or Item3. We can easily do this using the Required Field Validator control.

Required Field Validator control have a property named “Initial Value” which can be set to “Select any item” for the item that you don’t want to be selected. Now if “Select any Item” is selected than you will receive a error message saying that “Please select a different Item”. You can display any error message that you want.

Compare Validator Control:

Compare validators are used when you need to compare two fields. It can most commonly be used when you need to confirm password entered by the user at the registration time. As you can see in the image below that the two fields are not equal. In order for the compare validator to return true both the fields must be exactly same. It is case sensitive that means “codersource” and “Codersource” are not equal. The button click code will be the same which checks for the Page.IsValid property to be true.

Range Validator Control:

Range validator control is used when you need to check for a certain range. Suppose you have a textbox and you want to check that user enters a number in the textbox that is greater than 25. For this purpose you can use the range validator control. The Range validator control exposes properties “Minimum Value” and “Maximum Value” which can be used to check for certain range. Let’s activate this range validators controls programmatically:

private void Page_Load(object sender, System.EventArgs e){
	RangeValidator1.MinimumValue = 25;
	RangeValidator1.MaximumValue = 500;
}

As you can see that using RangeValidator control is pretty simple. We just set the MinimumValue and the MaximumValue and now if the user enters a number which is less than or greater than MinimumValue and MaximumValue respectively he will receive an error message.

Regular Expression Validator Control:

Regular Expression Validator is the coolest of all validators controls. It is used to validate complex expressions. These expressions can be phone number, email address, zip code and many more. Using Regular Expression Validator is also very straight forward. Simply set the ValidationExpression property to any type of expression you want and it will validate it. If you find that it does not contain the expression that you are looking for than you can make the expression yourself by using the using System.Text.RegularExpressions; namespace.

Validation Summary Control:

Validation Summary Control is used to display the validation error messages in one place. Suppose you want to display all the validation messages in one page to make the page look better and cleaner than you can use Validation Summary Control. In order to work with the Validation Summary Control you must set the Text property of the other validation controls. If you don’t set the Text property of the other Validation controls than the validation control won’t know that what to display when any validation error occurs. So what ever you put in the Text property of the validation control it will be displayed in the Validation Summary control.

Mixing different Expressions:

Its a common practice to mix different validation controls together to make a more strong validation. Suppose you have a textbox on the page which you want to validate as an email address and that is also a required field. So first of all you will use a Required Field Validator control so that the field is not left blank and than you will use the Regular Expression Validator to validate it against the email address expression.

Additional Tip:

Here is a very cool tip for you. Suppose you are using many user controls on a single page. Each user control has a button but you don’t want the validation to be fired when the button from the user control is clicked. For this you can set the CausesValidation property to false for the button that you don’t want the validation to fire.

Attachments:

Project Files: Validation_controls.zip