Validation Control in ASP.NET
M.SARULATHAAssistant ProfessorDept. of CA, JJC.
RequiredFieldValidation Control
The RequiredFieldValidator control
is simple validation control, which checks to see if the data is entered for
the input control. You can have a RequiredFieldValidator control for each form
element on which you wish to enforce Mandatory Field rule.
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
Style="top: 98px; left: 367px; position: absolute; height: 26px; width: 162px"
ErrorMessage="password required" ControlToValidate="TextBox2">
</asp:RequiredFieldValidator>
CompareValidator Control
The CompareValidator control allows
you to make comparison to compare data entered in an input control with a
constant value or a value in a different control.
It can most commonly be used when
you need to confirm password entered by the user at the registration time. The
data is always case sensitive.
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" Style="top: 145px;
left: 367px; position: absolute; height: 26px; width: 162px" ErrorMessage="password required"
ControlToValidate="TextBox3"></asp:RequiredFieldValidator>
RangeValidator Control
The RangeValidator Server Control is
another validator control, which checks to see if a control value is within a
valid range. The attributes that are necessary to this control are:
MaximumValue, MinimumValue, and Type.
<asp:RangeValidator ID="RangeValidator1" runat="server"
Style="top: 194px; left: 365px; position: absolute; height: 22px; width: 105px"
ErrorMessage="RangeValidator" ControlToValidate="TextBox4" MaximumValue="100" MinimumValue="18" Type="Integer"></asp:RangeValidator>
RegularExpressionValidator Control
A regular expression is a powerful
pattern matching language that can be used to identify simple and complex
characters sequence that would otherwise require writing code to perform.
Using RegularExpressionValidator
server control, you can check a user's input based on a pattern that you define
using a regular expression.
It is used to validate complex
expressions. These expressions can be phone number, email address, zip code and
many more. Using Regular Expression Validator is very simple. Simply set the
ValidationExpression property to any type of expression you want and it will
validate it.
If you don't find your desired
regular expression, you can create your custom one.
In
the example I have checked the email id format:
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" Style="top: 234px;
left: 366px; position: absolute; height: 22px; width: 177px"
ErrorMessage="RegularExpressionValidator" ControlToValidate="TextBox5"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
The
complete code for the above 4 controls is as,
Default.aspx
Design

Default.aspx
Source code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label3" runat="server" Style="top: 241px; left: 70px; position: absolute;
height: 22px; width: 128px; bottom: 282px;" Text="Enter your email id:"></asp:Label>
<asp:Label ID="Label1" runat="server" Style="top: 54px; left: 74px; position: absolute;
height: 22px; width: 128px" Text="Enter your name:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Style="top: 54px; left: 221px; position: absolute;
height: 22px; width: 128px; right: 396px;"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Style="top: 56px;
left: 378px; position: absolute; height: 22px; width: 128px" ErrorMessage="RequiredFieldValidator"
ControlToValidate="TextBox1">name is
mandatory </asp:RequiredFieldValidator>
</div>
<p>
<asp:Button ID="Button1" runat="server" Style="top: 311px; left: 267px; position: absolute;
height: 26px; width: 61px" Text="Submit" />
</p>
<asp:TextBox ID="TextBox3" runat="server" Style="top: 145px; left: 217px; position: absolute;
height: 22px; width: 131px" TextMode="Password"></asp:TextBox>
<p>
<asp:TextBox ID="TextBox2" runat="server" Style="top: 101px; left: 218px; position: absolute;
height: 22px; width: 131px" TextMode="Password"></asp:TextBox>
<asp:Label ID="Label4" runat="server" Style="top: 105px; left: 74px; position: absolute;
height: 22px; width: 128px" Text="Password"></asp:Label>
<asp:TextBox ID="TextBox5" runat="server" Style="top: 239px; left: 210px; position: absolute;
height: 22px; width: 134px"></asp:TextBox>
</p>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" Style="top: 98px;
left: 367px; position: absolute; height: 26px; width: 162px" ErrorMessage="password required"
ControlToValidate="TextBox2"></asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" Style="top: 145px;
left: 367px; position: absolute; height: 26px; width: 162px" ErrorMessage="password required"
ControlToValidate="TextBox3"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator1" runat="server" Style="top: 149px; left: 512px;
position: absolute; height: 26px; width: 162px" ErrorMessage="CompareValidator"
ControlToValidate="TextBox3" ValueToCompare="hello"></asp:CompareValidator>
<p>
<asp:Label ID="Label5" runat="server" Style="top: 148px; left: 71px; position: absolute;
height: 22px; width: 128px; bottom: 375px;" Text="Confirm Password"></asp:Label>
<asp:TextBox ID="TextBox4" runat="server" Style="top: 194px; left: 212px; position: absolute;
height: 22px; width: 140px"></asp:TextBox>
<asp:Label ID="Label6" runat="server" Style="top: 194px; left: 71px; position: absolute;
height: 22px; width: 128px; bottom: 329px;" Text="Enter your age:"></asp:Label>
</p>
<asp:RangeValidator ID="RangeValidator1" runat="server" Style="top: 194px; left: 365px;
position: absolute; height: 22px; width: 105px" ErrorMessage="RangeValidator"
ControlToValidate="TextBox4" MaximumValue="100" MinimumValue="18" Type="Integer"></asp:RangeValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" Style="top: 234px;
left: 366px; position: absolute; height: 22px; width: 177px"
ErrorMessage="RegularExpressionValidator" ControlToValidate="TextBox5"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
</form>
</body>
</html>
CustomValidator
Control
You can solve your purpose with
ASP.NET validation control. But if you still don't find solution you can create
your own custom validator control.
The CustomValidator Control can be
used on client side and server side. JavaScript is used to do client validation
and you can use any .NET language to do server side validation.
I will explain you CustomValidator
using server side. You should rely more on server side validation.
To
write CustomValidator on server side you override ServerValidate event.

Code
behind file
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void UserCustomValidate(object source, ServerValidateEventArgs args)
{
string str = args.Value;
args.IsValid = false;
//checking for input length greater than 6 and less than 25 characters
if (str.Length < 6 || str.Length > 25)
{
return;
}
//checking for a atleast a single capital letter
bool capital = false;
foreach (char ch in str)
{
if (ch >= 'A' && ch <= 'Z')
{
capital = true;
break;
}
}
if (!capital)
{
return;
}
//checking for a atleast a single lower letter
bool lower = false;
foreach (char ch in str)
{
if (ch >= 'a' && ch <= 'z')
{
lower = true;
break;
}
}
if (!lower)
{
return;
}
bool digit = false;
foreach (char ch in str)
{
if (ch >= '0' && ch <= '9')
{
digit = true;
break;
}
}
if (!digit)
{
return;
}
args.IsValid = true;
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
}
}

The server side validation you write
does not need to provide the exact same validation as that of the client side
validation. The client side validation can check for the user input data for
range and type and server side validation can check for matching of data with
database. Both server side and client side validation can be used for total
solution.
Comments
Post a Comment