aspsite.com

Using the ASP.NET RequiredFieldValidator Control



The RequiredFieldValidator control enables you to check whether or not a user has supplied a value for a form field. If a user has not entered a value, an error message is displayed.

Requiring a Form Field

There are two properties that you must set in order to use the RequiredFieldValidator control:

ControlToValidate - The name (ID) of the control being validated.

Text - The error message displayed when the field does not have a value.

The following page illustrates how you can use the RequiredFieldValidator control with two TextBox controls named txtFirstName and txtLastName.

Specifying an Initial Value

You can specify an initial value for the value in a TextBox. If a user submits the TextBox with its initial value, then the RequiredFieldValidator displays an error message.

For example, the following page contains a multi-line TextBox with the default text "Enter your comments here". The RequiredFieldValidator has the same text assigned to its InitialValue attribute.

aspsite.com

© 1997-2011 aspsite.com All Rights Reserved.