Export (0) Print
Expand All

How to: Validate Required Entries for ASP.NET Server Controls 

You can specify that a user must provide information in a specific control on an ASP.NET Web page by adding a RequiredFieldValidator control to the page and linking it to the required control. For example, you can specify that users must fill in a Name text box before they can submit a registration form.

If validation is being performed on the client, the user can leave a required field blank (or with its default value) while working in the page, but must provide a non-default value before submitting the page. However, once a value has been entered in the field, the user cannot clear the field (or return it to its default value). 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 page is submitted, so the user will not see an error message until after the page is submitted.

NoteNote

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.

To validate a required entry

  1. Add a RequiredFieldValidator control to the page and set the following properties:

    Property Description

    ControlToValidate

    The ID of the control for which the user must provide a value.

    ErrorMessage, Text, Display

    Properties that specify the text and location of the error or errors that will appear if the user skips the control. For details, see How to: Control Validation Error Message Display for ASP.NET Server Controls.

  2. Add a test in your ASP.NET Web page code to check for validity. For details, see How to: Test Validity Programmatically for ASP.NET Server Controls.

    The following example shows the .aspx file of a TextBox server control with required field validation.

    Security noteSecurity Note

    This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

    No code example is currently available or this language may not be supported.

See Also

Community Additions

ADD
Show:
© 2014 Microsoft