0% found this document useful (0 votes)
63 views7 pages

The Requirement Filed Validator Control

The RequiredFieldValidator control ensures that users enter required data in input controls like TextBox. It displays an error message if required data is not entered. The RangeValidator control checks if a value is within a specified minimum and maximum range, and can validate numbers, strings, and dates. An example application demonstrates using these controls to validate name, age, and date of birth fields. Users must enter data in all fields to submit the form successfully.

Uploaded by

Mohamed Ilyas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views7 pages

The Requirement Filed Validator Control

The RequiredFieldValidator control ensures that users enter required data in input controls like TextBox. It displays an error message if required data is not entered. The RangeValidator control checks if a value is within a specified minimum and maximum range, and can validate numbers, strings, and dates. An example application demonstrates using these controls to validate name, age, and date of birth fields. Users must enter data in all fields to submit the form successfully.

Uploaded by

Mohamed Ilyas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

The RequiredFieldValidator 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:

Table 5.3: Noteworthy Public Property of the RequiredFieldValidator Class


Property Description

InitialValue Handles the initial value of the associated input


control

Le next section, let's create a Web application to learn the usage of the RequiredFieldValidator

Using the RequiredFieldValidator Control


our can create a Web application to see the usage of RequiredFiledValidator control. Let's perform
the blowing steps to use the RequiredFieldValidator controlin a Web application

1. Click Start->All Programs->Microsoft Visual Studio 2012 Visual Studio 2012

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.

7. Click the OK button.

The Requiredfieldvalidator Web application is created.

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:

Listing 5.1: Showing the Code of WebForm1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind-"webForm1.aspx.cs Inherits


"Requiredfieldvalidator.webForm1" %>
<1DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"> <title/title> </head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Enter Name: "></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldvalidator ID="RequiredFieldValidator1" runat="s "server"
ControlToValidate="TextBox!" ErrorMessage=Please enter your name">
</ asp:RequiredFieldValidator> <br />
<asp:Label ID="Labe 12" runat="server" Text="Enter Lastname: "></asp:Label>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RequiredFieldvalidator ID="RequiredFieldvalidator2" runat="server" Control
Tovalidate-"TextBox2" ErrorMessage="*Please enter your lastname">
</asp:RequiredFieldvalidator> <br />
<asp:Button ID="Button1" runat="server" Text="Submit" />
</div>
</form>
</body>
</html>

You must add the following code snippet in the <configuration> tag in Web.config file of the
application:

<oppSettings> <add key="ValidationSettings: UnobtrusiveValidationMode" value="None" />


</appSettings>

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:

Now, let's know about the RangeValidator control.

The RangeValidator Control


The RangeValidator Control
The RangeValidator control checks whether or not the value of an input control lies within a
specified range of values. The following are the properties of the RangeValidator control:

ControlToValidate: Contains the input control to validate

MinimumValue: Holds the minimum value of the valid range

MaximumValue: Holds the maximum value of the valid range

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

Table 5.4 lists the noteworthy public


properties of the RangeValidator class:
Property Description

MaximumValue Obtains or sets the maximum value of the


validation range

MinimumValue Obtains or sets the minimum value of the


validation range

You can also use it to validate a range of numbers, strings, and dates.

Using the RangeValidator Control


You can create an application to learn the usage of RangeValidator control. Let's perform the
following steps to learn the use of the RangeValidator control in a Web application:

1. Open Visual Studio 2012.

2. Select FILE→New→Project option from the menu bar. The New Project dialog box appears.

3. Select Installed→Templates Visual C#→Web option in the left pane.

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.

6. Click the OK button. The Rangevalidator Web application is created.

7. Add a Web form to the Rangevalidator Web application.

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:

Listing 5.2: Showing the Code of WebForm1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="webForm1.aspx.cs"


Inherits="Rangevalidator.webForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"> <title></title> </head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Range validator Example"></asp:Label>
<br /> <br />
<asp:Label ID="Label2" runat="server" Text="Your Age "></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Rangevalidator ID="Rangevalidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="Age should be between 18 to 99">
</asp:Rangevalidator> <br /> <br />
<asp:Label ID="Label3" runat="server" Text="Date of birth (YYYY/MM/DD format)">
</asp:Labels>
<asp:TextBox ID-"TextBox2" runat="server"></asp:TextBox>
<asp:Rangevalidator 10-"Rangevalidator2" runat"server”
centreltovalidate TextBox2" ErrorMessage-"Enter a date between 1900/3/3 to 2012/3
</ asp:Rangevalidator> <br /> <br />
< asp:Button ID="Button1" runat="server" Text="Subelt" /></div>
</form>
</body>
</html>

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

You might also like