protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args )
{
}
using System;
using System.Web.UI.WebControls;
public partial class CustomValidation : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args )
{
string inputData = args.Value;
args.IsValid = false;
if (inputData.Length < 6 || inputData.Length > 12) return;
bool upperCase = false;
foreach (char ch in inputData)
{
if (ch >= 'A' && ch <= 'Z')
{
upperCase = true;
break;
}
}
if (!upperCase) return;
bool lowerCase = false;
foreach (char ch in inputData)
{
if (ch >= 'a' && ch <= 'z')
{
lowerCase = true; break;
}
}
if (!lowerCase) return;
bool number = false;
foreach (char ch in inputData)
{
if (ch >= '0' && ch <= '9')
{
number = true; break;
}
}
if (!number) return;
args.IsValid = true;
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CustomValidation.aspx.cs" Inherits="CustomValidation" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Password"></asp:Label>
<asp:TextBox ID="txtPassword" runat="server" Width="143px" ToolTip="Password must be between 6-12 characters and include 1 capital letter, 1 lowercase letter, and 1 number"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="txtPassword"
ErrorMessage="Password must be between 6-12 characters and include 1 capital letter, 1 lowercase letter, and 1 number" OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
<br />
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Login" />
</div>
</form>
</body>
</html>