<asp:TextBox ID="dob" runat="server"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Invalid Age, maximum age is 17 years!"
ClientValidationFunction="agecheck" ControlToValidate="dob"></asp:CustomValidator>
</body>
<script type="text/javascript" language="javascript">
function agecheck(sender, args) {
var dt = new Date();
var maxdate = new Date("December 31, 2011");
var dob = new Date(document.getElementById('<%=dob.ClientID %>').value);
var years = maxdate.getFullYear() - dob.getFullYear();
//check to see if less than 17 years old
if (years < 17 && years >= 0) {
args.IsValid = true;
return;
}
//if exactley 17 years old check to see if birthday month has passed
if (years == 17) {
if (dt.getMonth() + 1 > dob.getMonth() + 1) {
args.IsValid = true;
return;
}
//if the months are equal check to see if birthday day has passed
if (dt.getMonth() + 1 == dob.getMonth() + 1) {
if (dt.getDate() > dob.getDate()) {
args.IsValid = true;
return;
}
}
}
args.IsValid = false;
}
</script>
You can make your Own Logic in calculating the Age and Verify in the above.
Thanks & Regards,
Rajasekhar.R