+91-90427 10472
         
Dot net training in Chennai -Maria Academy

Asp.net Validation Controls

20 Oct 2016

Document by Ganesan  -ganesanva@hotmail.com – + 919600370429
List of Asp.net Validation available are as follows,

1.Required Field validator
2.Regular Expression validator
3.Range Validator
4.Custom Validator
5.Compare Validator

Create a new Asp.net Web application
File -> New Project
b2-1
Right click on the solution and Add New web form page AddStudentDetails.aspx as below,
b2-2
Replace the HTML Tag in  AddStudentDetails.aspx with below snippet,

<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
<title></title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<table border=”0″ cellspacing=”2″ cellpadding=”2″>
<tr>
<td>Student Name
</td>
<td>
<asp:TextBox ID=”txtStudentName” runat=”server”></asp:TextBox>
<asp:RequiredFieldValidator ID=”RQStudentName” runat=”server” ControlToValidate=”txtStudentName” ErrorMessage=”Please enter Student Name” ForeColor=”Red”>
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td> Date of Birth
</td>
<td>
<asp:TextBox ID=”txtDOB” runat=”server”></asp:TextBox>
<asp:RangeValidator ID=”Rvalid” runat=”server”
ControlToValidate=”txtDOB” ErrorMessage=”Date of Birth should not be greater than current date” Type=”Date” ForeColor=”Red”></asp:RangeValidator>
</td>
</tr>
<tr>
<td> Age
</td>
<td>
<asp:TextBox ID=”txtAge” runat=”server”></asp:TextBox>
<asp:RequiredFieldValidator ID=”RqAge” runat=”server” ControlToValidate=”txtAge” ErrorMessage=”Please enter Student Age”  ForeColor=”Red”>
</asp:RequiredFieldValidator>
<asp:RangeValidator ID=”RgAge” runat=”server”  ControlToValidate=”txtAge”  ErrorMessage=”Age should be greater than 18″ MinimumValue=”18″ ForeColor=”Red”
MaximumValue=”2000″ Type=”Integer”></asp:RangeValidator>
</td>
</tr>
<tr>
<td> City
</td>
<td>
<asp:DropDownList ID=”ddlCity” runat=”server”>
<asp:ListItem Text=”–Select–” Value=”–Select–“></asp:ListItem>
<asp:ListItem Text=”Chennai” Value=”Chennai”></asp:ListItem>
<asp:ListItem Text=”Madurai” Value=”Madurai”></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID=”RqCity” runat=”server” InitialValue=”–Select–” ControlToValidate=”ddlCity”
ErrorMessage=”Please enter Student City” ForeColor=”Red”>
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td> Email Id
</td>
<td>
<asp:TextBox ID=”txtEmailId” runat=”server”></asp:TextBox>
<asp:RequiredFieldValidator ID=”RqEmailId” runat=”server” ControlToValidate=”txtEmailId” ErrorMessage=”Please enter Email ID”  ForeColor=”Red”>
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID=”REVEmailId” runat=”server” ErrorMessage=”please enter correct email id” ControlToValidate=”txtEmailId”
ValidationExpression=”\w+([-+.’]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*” ForeColor=”Red”></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td> User Name
</td>
<td>
<asp:TextBox ID=”txtUserName” runat=”server”></asp:TextBox>
<asp:RequiredFieldValidator ID=”RequiredFieldValidator1″ runat=”server” ControlToValidate=”txtUserName” ErrorMessage=”Please enter UserName”
ForeColor=”Red”>
</asp:RequiredFieldValidator>
<asp:CustomValidator ID=”CVUserName” runat=”server” ControlToValidate=”txtUserName” OnServerValidate=”CVUserName_ServerValidate”
ErrorMessage=”UserName should not be greater than 15 digits” ForeColor=”Red”></asp:CustomValidator>
</td>
</tr>
<tr>
<td> Password
</td>
<td>
<asp:TextBox ID=”txtPassword” runat=”server” TextMode=”Password”></asp:TextBox>
<asp:RequiredFieldValidator ID=”RequiredFieldValidator2″ runat=”server” ControlToValidate=”txtPassword” ErrorMessage=”Please enter Password”  ForeColor=”Red”>
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td> Retype Password
</td>
<td>
<asp:TextBox ID=”txtRetypePassword” runat=”server” TextMode=”Password”></asp:TextBox>
<asp:RequiredFieldValidator ID=”RequiredFieldValidator3″ runat=”server” ControlToValidate=”txtRetypePassword” ErrorMessage=”Please enter Retype Password”  ForeColor=”Red”>
</asp:RequiredFieldValidator>
<asp:CompareValidator ID=”cmpPasswordmatch” runat=”server” ControlToCompare=”txtPassword” ControlToValidate=”txtRetypePassword”
ErrorMessage=”Password and Retype Password should not match” ForeColor=”Red”>
</asp:CompareValidator>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID=”btnSave” runat=”server” Text=”Save” CausesValidation=”true” OnClick=”btnSave_Click” />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

Replace the AddStudentDetails.cs class with the below snippet,

public partial class AddStudentDetails : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Rvalid.MinimumValue = DateTime.MinValue.ToString(“dd-MM-yyyy”);
Rvalid.MaximumValue = DateTime.Now.ToString(“dd-MM-yyyy”);
}
protected void CVUserName_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = (args.Value.Length < 15);
}
protected void btnSave_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
Response.Write(“Saved”);
}
}
}

The Output as below,
b2-3
b2-4
b2-5
Custom Validation on server event,
b2-6
Click below to download the solution,
https://1drv.ms/u/s!ArddhCoxftkQg6kmic1tyrHNsIbXqA

Social tagging: > > > > > > > > > >