Click here to Skip to main content
15,610,761 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The below code gave error message for any date, i need to display error message for below 15 years age from the current date please help me

<asp:Label ID="Label2" runat="server" Text="DOB"></asp:Label>
  <asp:TextBox ID="DOB" runat="server"></asp:TextBox>
  <asp:RequiredFieldValidator ID="RFVdob" runat="server" ControlToValidate="DOB" ErrorMessage="Enter Birth Date" Font-Bold="True" ForeColor="Red"></asp:RequiredFieldValidator>
  <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="DOB"    ValidationExpression="^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$" ErrorMessage="Enter a Valid Date (mm/dd/yyyy)" Font-Bold="True" ForeColor="Red" ></asp:RegularExpressionValidator>

   <asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="DOB"  ErrorMessage="Age above 15 " Type="Date"></asp:RangeValidator>

Code in Page Lode:

System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("hu-HU");
        RangeValidator1.MinimumValue = DateTime.Today.AddYears(-15).ToShortDateString();
        RangeValidator1.MaximumValue = DateTime.Today.ToShortDateString();
Richard C Bishop 12-Aug-13 12:59pm    
What error message would that be?
surendra4u 12-Aug-13 13:21pm    
Validation error message "Age above 15"
Richard C Bishop 12-Aug-13 13:45pm    
Oh ok, so it is working to the extent that it compiles and the validator is triggered. Your problem is that you get the error for any date?

Try putting the MinimumValue and MaximumValue inside the RangeValidator tag and removing them from the page load event handler.
surendra4u 12-Aug-13 13:50pm    
yes, any idea
Richard C Bishop 12-Aug-13 13:51pm    
I edited my response above, see it please.

1 solution

I had a similar requirement before to validate for age 18 and over.

I used a custom validator (i try to avoid regular expressions if i can)
<asp:CustomValidator ID="cvDOB" runat="server" ControlToValidate="pmDOB"
    ErrorMessage="Birth Date is under age 18."

Here is the javascript to check for age over 18
function VAge(obj, args) {
        //validate that a DOB is at least 18 years old.
        var today = new Date();
        var today_year = today.getFullYear();
        var value = Trim(args.Value);
        value = value.replace("-", "/").replace(".", "/");
        var splitDate = value.split("/");
        var dob_year = splitDate[2];
        var dob_day = splitDate[1];
        var dob_month = splitDate[0];
        var dob;

        dob = new Date(dob_year, dob_month - 1, dob_day); // as months are 0-11
        if ((dob_day == dob.getDate()) && (dob_month == (dob.getMonth() + 1)) && (dob_year == dob.getFullYear())) {
            dob_month = dob_month - 1; // as months are 0-11
            var age = today_year - dob_year;
            var today_month = today.getMonth();
            var today_day = today.getDate();
            if (dob_month > today_month) { age = age - 1 }  // next birthday not yet reached
            else if (dob_month == today_month && today_day < dob_day) { age = age - 1 }  // next birthday not yet reached
            if (age < 18) {
                args.IsValid = false;
        else {
            args.IsValid = false;

        args.IsValid = true;
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900