RequiredFieldValidator control in ASP.NET

In this article we will learn how to use RequiredFieldValidator control in ASP. NET.
  • 1786

In this article we will learn how to use RequiredFieldValidator control in ASP. NET.

RequiredFieldValidator control:

Validation control are Used to validate data inside the controls. The RequiredFieldValidator control ensures that the user does not empty or skip an entry inside the control. The control fails validation if the value it contains does not change from its initial value when validation is performed. By default, the initial value is an empty string ("").

Properties: RequiredField validations has the following properties.

rfv1.gif
 

Figure 1.

Text  - Text to display for the validator when the validated control is invalid.

BackColor - The background color of the RequiredFieldValidator control.

ControlToValidate - The id of the control to validate.

Display - The display behavior for the validation control. Legal values are:

-None (the control is not displayed. Used to show the error message only in the ValidationSummary control).

-Static (the control displays an error message if validation fails. Space is reserved on the page for the message even if the input passes validation.

-Dynamic (the control displays an error message if validation fails. Space is not reserved on the page for the message if the input passes validation.

ErrorMessage - The text to display in the ValidationSummary control when validation fails. Note: This text will also be displayed in the validation control if the Text property is not set.

ForeColor - The foreground color of the control.

InitialValue - Specifies the starting value of the input control. Default value is "".

For example:

Drag a Label, one Textbox, one Button and a RequiredFieldValidator control from the toolbox on the form. Form looks like this.

rfv2.gif
 

Figure 2.

Now set the following properties.

ControlToValidate -> TextBox1.

ErrorMessage  -> Please inter the name.

Now save and run the application.

rfv3.gif
 

Figure 3.

Now we click on the Button(validate) without write anything in the TextBox. It display an error. such as

rfv4.gif
Figure 4.
 

Now enter the name in the TextBox and then click on the button it will not display the error message.

rfv5.gif
 

Figure 5.


Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.