ASP.NET Web Forms Web Forms Server Controls Web Forms Validation
You can specify that a user must provide information in a specific control.
- Add a RequiredFieldValidator control to the page and set the following properties:
Property |
Setting |
ControlToValidate |
The ID of the input control that the validation control will evaluate. |
Display |
The display behavior for the specified validation control. |
ErrorMessage |
The error message to display in the ValidationSummary control if validation fails. |
Text |
The error message to display in the validation control when validation fails. |
- In your Web Forms code, check for validity. For details, see Testing Validity Programmatically.
If validation is being performed on the client, the user can leave a required field blank while working in the page, but must fill it in before submitting. However, once a value has been filled into the field, the user cannot clear the field. If the field is cleared, the user sees an error message immediately when leaving the field. In server-side validation, no checking is done until the form is submitted, so the user will not see an error message until after the form is submitted.
For details on displaying error messages, see Controlling Validation Message Display.
NOTE: Required-entry validation is frequently used in conjunction with other types of validation. You can use as many validation controls for a user-entry field as needed.
The following shows an example of using a RequiredFieldValidator with a TextBox control.
<asp:textbox id="txtLastName" runat="server" />
<asp:requiredfieldvalidator id="myReqFldValidator" runat="server"
controltovalidate="txtLastName"
errormessage="Last name is a required field."
forecolor="navy" />
Types of Validation RequiredFieldValidator Control RequiredFieldValidator Class Data Entry Validation ( Client-Side )