HTML
<!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 id="Head1" runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
color: #FF9900;
}
.style3
{
color: Teal;
}
</style>
<script language="javascript">
function validateLength(sender, args) {
if (args.Value.length <= 20)
args.IsValid = true;
else
args.IsValid = false;
}
</script>
<script type="text/javascript" language="javascript">
function CustomValidate(sender, args) {
if (document.getElementById('drpAdmission').selectedIndex > document.getElementById('drpPassingdate').selectedIndex) {
args.IsValid = false;
}
else {
args.IsValid = true;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<span class="style3"><b style="font-weight: bold; font-size: x-large">Client Side Custom
Validation First example </b></span> <table class="style1">
<tr>
<td width="150">
</td>
<td width="5px">
</td>
<td class="style2">
</td>
</tr>
<tr>
<td width="150" align="char">
<strong>Enter text to test </strong>
</td>
<td width="5px">
:
</td>
<td>
<asp:TextBox ID="txtParagraph" runat="server" Height="50px" TextMode="MultiLine"
Width="250px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
<asp:Button runat="server" ID="btnSubmit" Text="Submit" CausesValidation="true" />
<asp:CustomValidator runat="server" ID="CVParagrph" ControlToValidate="txtParagraph"
ForeColor="Red" ClientValidationFunction="validateLength" Text="Only 25 Cahracter is allowed to input">
</asp:CustomValidator>
</td>
</tr>
</table>
<span class="style3"><b style="font-weight: bold; font-size: x-large">Client Side Custom
Validation Second example</b></span>
<table class="style1">
<tr>
<td width="150">
</td>
<td width="5px">
</td>
<td class="style2">
</td>
</tr>
<tr>
<td width="150">
<strong>Admission Date </strong>
</td>
<td width="5px">
:
</td>
<td>
<asp:DropDownList ID="drpAdmission" runat="server" Width="100px" ValidationGroup="year">
</asp:DropDownList>
</td>
</tr>
<tr>
<td width="150">
<strong>Passing Year</strong>
</td>
<td width="5px">
:
</td>
<td>
<asp:DropDownList ID="drpPassingdate" runat="server" Width="100px" ValidationGroup="year">
</asp:DropDownList>
</td>
</tr>
<tr>
<td width="150">
</td>
<td width="5px">
</td>
<td>
<asp:CustomValidator ID="custvaddropdown" runat="server" ValidationGroup="year" ClientValidationFunction="CustomValidate"
ControlToValidate="drpAdmission" ErrorMessage="Admission date must be less then passing date"
Text="Admission date must be less then passing date." ForeColor="red"></asp:CustomValidator>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
<asp:Button ID="btntest" runat="server" Text="Year Test" ValidationGroup="year" />
</td>
</tr>
</table>
</form>
</body>
</html>
CODE BEHIND
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class TestDesign_CustomValidator : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadYear();
}
}
private void LoadYear()
{
for (int i = 1985; i < 2016; i++ )
{
drpAdmission.Items.Add(i.ToString());
drpPassingdate.Items.Add(i.ToString());
}
}
}