The Requirement Filed Validator Control
The Requirement Filed Validator Control
The RequiredFieldValidator control ensures that the user has entered the required data in the input
control, such as the TextBox control, to which it is bound. For example, if you are entering data to
buy a T-shirt in an input control, then it is necessary to enter the size of the T-shirt that you want to
buy. if you do not enter the size, the validation control displays an error message.
Table 5.3 lists the noteworthy public property of the RequiredFieldValidator class:
Le next section, let's create a Web application to learn the usage of the RequiredFieldValidator
2. Select FILE->New->Project option from the menu bor. The New Project dialog box appears.
3. Select Installed Templates>Visual C# Web option in the left pane of the New Project dialog box
4. Select the ASP.NET Empty Web Application template in the middle pane of the New Project dialog
box.
5. Enter Requiredfieldvalidator in the Name text box to specify the name of the application.
6. Enter an appropriate location for the application in the Location combo box.
8. Open the Solution Explorer window and right-click the application name. In our case, we are right-
clicking the Requiredfieldvalidator Web application.
9. Select the Add > New Item option from the context menu. The Add New Item
Requiredfieldvalidator dialog box appears.
10. Select the Web form option from the middle pane.
11. Click the Add button.
The WebForm1.aspx form is added to the Requiredfieldvalidator Web application.
12. Add two Label controls, two TextBox controls, and one Button control from the Toolbox to the
Web form.
13. Open the Properties window and set the Text properties of the Label1 control as Enter Nama-
Label2 as Enter Lastname:, and Button1 control as Submit.
14. Drag and drop two RequiredFieldValidator controls on the Web form from the Toolbox.
15. Open the Properties window and bind the ControlToValidate property of
RequiredFieldValidator1 control with the TextBox 1 control, and set the error message in the
ErrorMessage property as ‘’please enter your name
Similarly, bind the ControlToValidate property of RequiredFieldValidator2 control with the TextBox2
control with the TextBox1 control, and set the error message in the ErrorMessage prop ty as "Please
enter your name.
After adding all the control to the web form, it appears as shown in Fig.ASP-5.1:
A Fig. ASP-5.2
Listing 5.1 shows the source code for the WebForm1.aspx form after setting the properties of the o
in the WebForm1.aspx form:
You must add the following code snippet in the <configuration> tag in Web.config file of the
application:
16. Press the F5 key to execute the Web application in the browser (Fig. ASP-5.2).
17. Click the Submit button without entering data in the two TextBox controls (Fig. ASP-5.2:)
Fig.ASP-5.2:
If one of these properties is set, then the other properties must also be set; otherwise, it gives error
messages. Do not forget to set the Type property to the data type of the input control to avoid error
due to type of data entered in the control. The different data types that can be used for the input
control values are String, Integer, Double, Date, and Currency.
Table 5.4: Noteworthy Public Properties of the RangeValidator Class
You can also use it to validate a range of numbers, strings, and dates.
2. Select FILE→New→Project option from the menu bar. The New Project dialog box appears.
4. Select the ASP.NET Empty Web Application template in the middle pane.
5. Enter Rangevalidator in the Name text box to specify the name of the application. In addition, 4
enter an appropriate location for the application in the Location text box.
8. Drag and drop three Label controls, two TextBox controls, and one Button control from Toolbox.
9. Open the Properties window and set the Text property of Label1 control as Range Vid Example,
Label 2 as Your Age, Label 3 control as Date of birth (YYYY/MM/DD formed, Button1 control as
Submit.
10. Drag and drop the two RangeValidator controls (RangeValidator] and Range Validator2) from
Toolbox on the Web form.
11. Open the Properties window and bind the ControlToValidate property of the RangeValidator c to
the TextBox1 control and set the ErrorMessage property as Age should be between 18 to 99
12. Set the MaximumValue property of the RangeValidator1 control to 99 and Minimum property to
18.
Similarly, bind the ControlToValidate property of the RangeValidator2 control to the TexBox control
and set the ErrorMessage property as Enter a date between 1900/1/1 to 2012/1/1. the
MaximumValue property of the RangeValidator2 control to 2012/1/1 and MinimumVolus
property to 1900/1/1.
After setting all the properties and arranging the controls accordingly, the Web form appears shown
in Fig.ASP-5.3:
Fig.ASP-5.3
Listing 5.2 shows the source code for the WebForm1.aspx form after adding all the controls in the
Web form:
You must add the following code snippet in the <configuration> fog in Web.config file of the
application:
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
</appSettings>
13. Press the F5 key to execute the Web application (Fig ASP-5.4).
14. Enter the values in the textboxes and click the Submit button, as shown in Fig ASP-5.4:
Fig.ASP-5.4