Validation Controls in Web Forms: Part 2

RangeValidator Control

As the name indicates, this control is used to validate that the user entry falls within a specified range. As with CompareValidator control, the validation can be against specific values or against values of controls.

The minimum value of the range is indicated by setting the MinimumValue or MinimumControl property. Similarly the maximum value is indicated by setting the MaximumValue or MaximumControl property.

Set the Type property using the ValidationDataType enumeration.

<script language="C#" runat="server">
void HiBtn_Click(Object Src, EventArgs E)
{
lblHello.Text = "Hi " + txtName.Text + ", Welcome to ASP.NET!";
}
</script>

<html>

<head>

<title>RangeValidator Control</title></head><body>

<form runat="server" ID="Form1">

Enter your name:
<asp:textbox id="txtName" runat="server"/>

<br/>

On a scale of 1 to 5 (1 being the lowest), how do you rate this article:
<asp:textbox id="txtRate" runat="server"/>

<
asp:RangeValidator id="RangeFieldValidator1" runat="server"
ForeColor="Red"

ControlToValidate="txtRate"

MinimumValue=1

MaximumValue=5

Type="Integer"

ErrorMessage="Please enter an integer in the range 1 to 5.">

</asp:RangeValidator >

<br/>

<asp:button text="Enter" Onclick="HiBtn_Click" runat=server ID="Button1"/>

<b><asp:Label id="lblHello" runat="server" /></b><br />

</form>

</body>

</html>

Listing 5: RangeValidator Control - Validate that the entered value lies within a range

ValidC5.jpg

Figure 5: RangeValidator Control - Validate that the entered value lies within a range

RegularExpression Control

This is a customized validation control that validates the user's entry matches a predefined pattern such as phone number, email address.

Visual Studio.Net provides some predefined patterns. Of course, you can specify your own expressions. Set the pattern to compare by setting the ValidationExpression property.

<script language="C#" runat="server">
void HiBtn_Click(Object Src, EventArgs E)
{
lblHello.Text = "Hi " + txtName.Text + ", Welcome to ASP.NET!";
}
</script>

<html>

<head>

<title>RegExValidator Control</title></head><body>

<form runat="server" ID="Form1">

Enter your name:
<asp:textbox id="txtName" runat="server"/>

<br/>

Enter your email address:
<asp:textbox id="txtEmail" runat="server"/>

<asp:RegularExpressionValidator id="RegExValidator1" runat="server"

ErrorMessage="Please Enter a Valid Email address" ControlToValidate="txtEmail"

ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">

</asp:RegularExpressionValidator>

<br/>

<asp:button text="Enter" Onclick="HiBtn_Click" runat=server ID="Button1"/>

<b><asp:Label id="lblHello" runat="server" /></b><br />

</form>

</body>

</html> 

Listing 6 : Regular Expression Validator Example  

ValidC10.jpg

Figure 6 : Regular Expression Validator 

CustomValidator Control

Validation requirements do not always fall into explicit patterns and can become complicated for even medium sized systems. The CustomValidator control is used to validate against any custom validation function created by you.

Create a ServerValidate event for the validation control and specify the custom logic for the validation. 

<script language="C#" runat="server">
protected void LikeValidate (object source, ServerValidateEventArgs args)
{
try
{
string str = Convert.ToString(args.Value);
args.IsValid = (str != "Yes") ? false : true;
}
catch
{
args.IsValid = false;
}
}
void HiBtn_Click(Object Src, EventArgs E)
{
if (Page.IsValid)
lblHello.Text = "Hi " + txtName.Text + ", Welcome to ASP.NET!";
}
</script>

<html>

<head>

<title>CustomValidator Control</title></head><body>

<form runat="server" ID="Form1">

Enter your name:
<asp:textbox id="txtName" runat="server"/>

<br/>

Do you like validation controls:
<asp:textbox id="txtLike" runat="server"/>

<asp:CustomValidator id="CustomValidator1" runat=server

ControlToValidate = "txtLike"

ErrorMessage = "You must enter Yes!"

onservervalidate="LikeValidate" >

</asp:CustomValidator> <br/>

<asp:button text="Enter" Onclick="HiBtn_Click" runat=server ID="Button1"/>

<b><asp:Label id="lblHello" runat="server" /></b><br />

</form>

</body>

</html> 

Listing 7: CustomValidator control 

ValidC11.jpg

Figure 7 : CustomValidator control  

How should I use Validation controls ?

The Validation Process

When the user clicks on any button control that has the CausesValidation property set to true, Page.Validate() gets invoked automatically. This instructs all the validation controls on the page to validate as per their property settings. The default value for the CausesValidation property is true.

You can also invoke the validation process any time in the code by calling the Validate function.

Each validation control sets a flag that can be checked programmatically and you can decide the logic flow based on the validity of controls.

Page.IsValid Property

The IsValid property of the page indicates if the page validation was successful. Refer to Listing 7 (CustomValidator example). Similarly, you can also check for the validity of individual controls by looping through the Page's Validators collection.

ValidationSummary control

The ValidationSummary control is used to summarize the all validation errors in the page. The DisplayMode property of the control provides options for formatting the display of the error information.

Summary

Once you start using Validation controls, you will find that you just cannot do without them !

icon_pencil.gif This article is updated (tested) to RTM by Brian Boyce.