Validation for the forms are used to avoid raw data to get stored in a database table. These validation is usually applied to text boxes on any form whether it is a Website or Windows application. We create an ASP.NET website and learn how to add different types of validators in a web page. Here is the step by step guide for ASP.NET Validation.
In ASP.NET, each of the entities we put on the web page are called as controls. Example TextBox control, Button control etc. Similarly, we have Validation controls. The advantage of creating an ASP.NET website is that, we do not need to write a code for validations. Validation is done by simply setting the validating conditions in the property fields of validation control. Following are the types of validators in ASP.NET:
This validator is used to ensure that user doesn’t keep the TextBox empty while filling the form.
- ControlToValidate: The most important property of any validator is ControlToValidate. You need to tell the validator which TextBox/Other Control it should look for. Usually it is set to TextBox’s.
- ErrorMessage: Indicates the error message to be shown if the corresponding TextBox is empty.
- ForeColor: You can set the color for these error messages so that these get highlighted.
CompareValidator is used to compare two values. Either it compares a value from one Control to fixed value or it compares the value from a Control to value from another Control.
- Type: This property specifies the type of data to be compared by the validator.
- ControlToValidate: Specifies the Control to be considered while comparing.
- ControlToCompare: Tells the Control(Ex. TextBox) to be compared with.
- ValueToCompare: Specifies the value to be compared with the Control value.
- Operator: This property tells the validator the type of operation it should carry out with the two values. Available operators are: Equal, NotEqual, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual, and DataTypeCheck. Most frequently used is Equal operator which is used to compare passwords in two TextBox’s while creating any account.
This validator makes sure that the value/text user enters in any TextBox falls within certain range.
- MinimumValue: This property is used to set the minimum value for the validator to check.
- MaximumValue: This property is used to set the maximum value for the validator to check.
- Type: In this property, we tell the type of the data to be entered. Example: Currency, Date, Double, Integer, and String.
This validator is used to check whether the data entered in the Text Box follows some fixed pattern. Normally used to make sure that user enters the Email address/Phone number in correct format.
- ControlToValidate: Specifies the Control to be considered while comparing for the regular expression.
- ValidationExpression: This property is set to the regular expression which it should match to. There are regular expressions available for email address, phone number and many more. Here in the below tutorial, we explained how to use RegularExpressionValidator for email address.
ValidationSummary control is not specific for any kind of validation, but it is used to display the summary of all the validation errors on a particular webpage. This control shows the content from ErrorMessage property of all other failed validators. Two main properties of this control are ShowSummary and ShowMessageBox.
Here is an example of a web page designed in Visual Studio 2010.
- We created ASP.NET Empty Website named WebsiteTutorial.
- Added ASP.NET folder named App_Data to this website.
- Added new Web Form named Student.aspx.
- And designed it as shown in the following diagram by using Labels, TextBox and Button.
- Also add the following validators:
- RequiredFieldValidator for all TextBox‘s
- Set ControlToValidate property of each validator to it’s corresponding TextBox’s.
- Set ErrorMessage property of each one by stating the error message to be shown of your choice. Ex: Enter Roll Number, Enter Name and Enter Email Id.
- CompareValidator for Password Confirmation.
- Set ControlToValidate property to TextBox4
- Set CompareToValidate property to TextBox5
- Set Type property to string
- Set Operator property to Equal.
- Set ErrorMessage property to Enter Matching Passwords
- RegularExpressionValidator for checking Email address field.
- Set ControlToValidate property to TextBox3
- Set ValidationExpression property to for email address format which is given there.
- Set ErrorMessage property to Example: firstname.lastname@example.org
- RequiredFieldValidator for all TextBox‘s
- So now when the Web Form is ready Debug the project (Debug -> Start Debugging)
- Your Web Form will be displayed in a browser.
- If you try to click on the Submit button by keeping the TextBox’s empty then, your RequiredFieldValidator will display the error messages as shown in the following diagram:
- Look at the RegularExpressionValidator and CompareValidator when we add email address which is not in correct format and mismatching passwords:
So, this is all about ASP.NET Validation. Try this tutorial and send us your feedback. Questions regarding the same are welcome.
To store the data entered in these TextBox’s in a database follow this tutorial: Insert Code C# from Visual Studio into SQL
- How To Create Setup file Visual Studio C#.NET Project - September 20, 2016
- How to Create a Simple ASP.NET Website-Master Pages - September 16, 2016
- Retrieve Code C# in Visual Studio 2010 - September 15, 2016
- ASP.NET Validation for a Website-Tutorial - August 31, 2016
- Insert Code C# from Visual Studio into SQL - August 26, 2016
- How to add Splash Screen to Windows App in VS10 C# - August 20, 2016