<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Validation Sample</title>
<style>
body
{
font-size: 12pt; font-family: Arial;
}
.ValidationMessage
{
font-color:red;
font-weight:bold;
}
</style>
<script language="javascript">
function isLengthValid(source,args)
{
args.IsValid=(args.Value.length >= 8);
}
</script>
</head>
<body>
<form id="Form1" method="post" runat="server">
<table border="0" cellpadding="2" cellspacing="0">
<tr>
<td colspan="3">
<b>Contact Information</b>
</td>
</tr>
<tr>
<td align="right" style="height: 27px">
First Name:
</td>
<td align="left" style="height: 27px">
<asp:TextBox ID="txtFirstName" MaxLength="25" Columns="25" runat="server" />
</td>
</tr>
<tr>
<td align="right">
Last Name:
</td>
<td align="left">
<asp:TextBox ID="txtLastName" MaxLength="40" Columns="40" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtLastName"
CssClass="ValidationMessage" ErrorMessage="Last Name">R</asp:RequiredFieldValidator></td>
</tr>
<tr>
<td align="right">
Address:
</td>
<td align="left">
<asp:TextBox ID="txtAddress" Columns="50" runat="server" />
</td>
</tr>
<tr>
<td align="right">
State:
</td>
<td align="left">
<asp:TextBox ID="txtState" Columns="2" MaxLength="2" runat="server" /> Postal
Code:
<asp:TextBox ID="txtPostalCode" Columns="10" MaxLength="10" runat="server" /> <asp:RegularExpressionValidator
ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtPostalCode"
CssClass="ValidationMessage" ErrorMessage="Postal Code" ValidationExpression="\d{5}(-\d{4})?">Postal code must be 5 numeric digits.</asp:RegularExpressionValidator></td>
</tr>
<tr>
<td align="right">
Phone:
</td>
<td align="left">
<asp:TextBox ID="txtPhone" Columns="20" MaxLength="20" runat="server" /> <asp:RequiredFieldValidator
ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtPhone" CssClass="ValidationMessage"
Display="Dynamic" ErrorMessage="Phone">R</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="txtPhone"
CssClass="ValidationMessage" ErrorMessage="Phone" ValidationExpression="(^x\s*[0-9]{5}$)|(^(\([1-9][0-9]{2}\)\s)?[1-9][0-9]{2}-[0-9]{4}(\sx\s*[0-9]{5})?$)">Phone must be in the format: (XXX) XXX-XXXX</asp:RegularExpressionValidator></td>
</tr>
<tr>
<td align="right">
Date of Birth:
</td>
<td align="left">
<asp:TextBox ID="txtDOB" Columns="10" MaxLength="10" runat="server" /> <asp:RequiredFieldValidator
ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtDOB" CssClass="ValidationMessage"
Display="Dynamic" ErrorMessage="Date of Birth">R</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ControlToValidate="txtDOB"
CssClass="ValidationMessage" Display="Dynamic" ErrorMessage="Date of Birth" ValidationExpression="^\d{1,2}\/\d{1,2}\/\d{4}$">Date must be in the format: mm/dd/yyyy</asp:RegularExpressionValidator>
<asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="txtDOB"
CssClass="ValidationMessage" ErrorMessage="Date of Birth" MinimumValue="1850/1/1"
Type="Date">Date must be between 1850 and today.</asp:RangeValidator></td>
</tr>
<tr>
<td colspan="2" style="height: 23px">
<b>Log-In</b>
</td>
</tr>
<tr>
<td align="right">
Email Address:
</td>
<td align="left">
<asp:TextBox ID="txtEmail" Columns="35" MaxLength="50" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtEmail"
CssClass="ValidationMessage" Display="Dynamic" ErrorMessage="Email">R</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" ControlToValidate="txtEmail"
CssClass="ValidationMessage" ErrorMessage="Email" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">Not a valid email format. Must be email@host.domain</asp:RegularExpressionValidator></td>
</tr>
<tr>
<td align="right">
Password:
</td>
<td align="left">
<asp:TextBox ID="txtPassword" TextMode="Password" MaxLength="50" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="txtPassword"
CssClass="ValidationMessage" Display="Dynamic" ErrorMessage="Password">R</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator5" runat="server" ControlToValidate="txtPassword"
CssClass="ValidationMessage" Display="Dynamic" ErrorMessage="Password" ValidationExpression=".*[!@#$%^&*+;:].*">Password must include one of these (!@#$%^&*+;:)</asp:RegularExpressionValidator>
<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="txtPassword"
CssClass="ValidationMessage" ErrorMessage="Password" ClientValidationFunction="isLengthValid" OnServerValidate="CustomValidator1_ServerValidate">Password must be 8 characters of greater.</asp:CustomValidator></td>
</tr>
<tr>
<td align="right">
Re-enter Password:
</td>
<td align="left">
<asp:TextBox ID="txtPasswordReEnter" TextMode="Password" MaxLength="50" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="txtPasswordReEnter"
CssClass="ValidationMessage" Display="Dynamic" ErrorMessage="Re-enter Password">R</asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="txtPassword"
ControlToValidate="txtPasswordReEnter" CssClass="ValidationMessage" ErrorMessage="Re-enter Password">Passwords do not match. Please re-enter.</asp:CompareValidator></td>
</tr>
<tr>
<td align="center" colspan="2">
<input id="Submit1" runat="server" type="submit" value="Login"></td>
</tr>
<tr>
<td colspan="2">
<asp:ValidationSummary ID="ValidationSummary1" runat="server" CssClass="ValidationMessage"
HeaderText="You must enter a valid value in the following fields:" />
</td>
</tr>
</table>
</form>
</body>
</html>
In Code :-
protected void Page_Load(object sender, EventArgs e)
{
//Set the MaximumValue for the range validator to today's date.
RangeValidator1.MaximumValue = DateTime.Today.ToString("yyyy/MM/dd");
}
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
//Password can not be < 8 characters.
args.IsValid = (args.Value.Length >= 8);
}
Thursday, July 9, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment