Validation Controls in ASP
Validation Controls in ASP
NET
Validation controls are used to,
1. Client Side
2. Serve Side
Client side validation is good but we have to be dependent on browser and scripting language support.
Client side validation is considered convenient for users as they get instant feedback. The main
advantage is that it prevents a page from being postback to the server until the client validation is
executed successfully.
For developer point of view serve side is preferable because it will not fail, it is not dependent on
browser and scripting language.
You can use ASP.NET validation, which will ensure client, and server validation. It work on both end;
first it will work on client validation and than on server validation. At any cost server validation will work
always whether client validation is executed or not. So you have a safety of validation check.
For client script .NET used JavaScript. WebUIValidation.js file is used for client validation by .NET
Validation Controls in ASP.NET
An important aspect of creating ASP.NET Web pages for user input is to be able to check that the
information users enter is valid. ASP.NET provides a set of validation controls that provide an easy-to-
use but powerful way to check for errors and, if necessary, display messages to the user.
1. RequiredFieldValidation Control
2. CompareValidator Control
3. RangeValidator Control
4. RegularExpressionValidator Control
5. CustomValidator Control
6. ValidationSummary
CompareValidator Compares the value of one input control to the value of another input
control or to a fixed value
RangeValidator Checks that the user enters a value that falls between two values
RegularExpressionValidator Ensures that the value of an input control matches a specified pattern
CustomValidator Allows you to write a method to handle the validation of the value entered
All validation controls are rendered in form as <span> (label are referred as <span> on client by server)
Validation Properties
Usually, Validation is invoked in response to user actions like clicking submit button or entering data.
Suppose you wish to perform validation on page when user clicks submit button.
When the value of the CausesValidation property is set to true, you can also use the ValidationGroup
property to specify the name of the validation group for which the Button control causes validation.
Page has a Validate() method. If it is true this methods is executed. Validate() executes each validation
control.
To make this happen, simply set the CauseValidation property to true for submit button as shown below:
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.
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.
<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>
Default.aspx Design
Default.aspx Source code
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.
Source Code
G]rfg[prpf[pf[gg;f;glage="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="Label1" runat="server" Text="User
ID:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server"
ControlToValidate="TextBox1" ErrorMessage="User id
required"></asp:RequiredFieldValidator>
<asp:CustomValidator ID="CustomValidator1" runat="server"
OnServerValidate="UserCustomValidate"
ControlToValidate="TextBox1"
ErrorMessage="User ID should have atleast a capital, small
and digit and should be greater than 5 and less
than 26 letters"
SetFocusOnError="True"></asp:CustomValidator>
</div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Submit" />
</form>
</body>
</html>
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)
{
}
}