RequiredFieldValidator Control in ASP.Net
Author: Faraz
In this article you will see the technique of validating the input made by end user. This can be done by using RequiredFieldValidator Control.
When you develop an ASP.Net form you will find that certain fields on that form must be entered by visitors. For example, you may have a form that requires visitors to enter a password or maybe you have a form where visitors need to enter a shipping address. Both of those fields would be required.
You require a field to have data in it on an ASP.Net page by using a RequiredFieldValidator control. That control requires visitors to enter something into the fields before submitting the form.
The web form used here shows you how to use that control. The page asks the visitors to enter their name and then press the OK button. If they leave the field blank, the form is not submitted to the server. Instead they see an error message letting them know that they must enter their name.
Add the following code on to your page:
The TextBox control is used by the end user to enter the name and on the button click the RequiredFieldValidator control is called and check for input. If the input is valid the form is processed, otherwise, it will report an error.
Note: You have to take care for two things:
- Specify the control which you want to validate using ControlToValidate property. Here I am using TextBox control.
- If you want to reserve the space for an error message when the error is generated, then set the Display property to Dynamic else you can use Static.