In this article I will explain with an example, how to validate Date of Birth (DOB) i.e. the Age entered in TextBox is minimum 18 years in ASP.Net using Regular Expression and Custom Validator.
There will be two types of Validations:
1. Date format dd/MM/yyyy validation: The Date of Birth (DOB) will be first validated for dd/MM/yyyy format using Regular Expression.
2. 18+ Minimum Age validation: The difference between the age entered in the TextBox and the Current Date is minimum 18 years.
 
 
HTML Markup
The following HTML Markup consists of an ASP.Net TextBox, a CustomValidator and a Button. The ClientValidationFunction property is set to ValidateDOB JavaScript function.
<asp:TextBox ID="txtDate" runat="server" Text="" />
<asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="ValidateDOB"
    ControlToValidate="txtDate" ErrorMessage="" ForeColor = "Red" />
<br />
<br />
<asp:Button ID="btnValidate" Text="Validate" runat="server"/>
 
 
The Date of Birth and 18+ Minimum Age JavaScript function
The entered date is fetched from the TextBox and is tested with the dd//MM/yyyy Date Format Regular Expression.
Once the date is verified, the date is split and converted into a JavaScript Date object.
The converted JavaScript object i.e. the Date of Birth is compared with the Current Date to verify whether the Age meets the minimum Age criteria i.e. 18 years or higher.
<script type="text/javascript">
    function ValidateDOB(sender, args) {
        //Get the date from the TextBox.
        var dateString = document.getElementById(sender.controltovalidate).value;
        var regex = /(((0|1)[0-9]|2[0-9]|3[0-1])\/(0[1-9]|1[0-2])\/((19|20)\d\d))$/;
 
        //Check whether valid dd/MM/yyyy Date Format.
        if (regex.test(dateString)) {
            var parts = dateString.split("/");
            var dtDOB = new Date(parts[1] + "/" + parts[0] + "/" + parts[2]);
            var dtCurrent = new Date();
            sender.innerHTML = "Eligibility 18 years ONLY."
            if (dtCurrent.getFullYear() - dtDOB.getFullYear() < 18) {
                args.IsValid = false;
                return;
            }
 
            if (dtCurrent.getFullYear() - dtDOB.getFullYear() == 18) {
 
                //CD: 11/06/2018 and DB: 15/07/2000. Will turned 18 on 15/07/2018.
                if (dtCurrent.getMonth() < dtDOB.getMonth()) {
                    args.IsValid = false;
                    return;
                }
                if (dtCurrent.getMonth() == dtDOB.getMonth()) {
                    //CD: 11/06/2018 and DB: 15/06/2000. Will turned 18 on 15/06/2018.
                    if (dtCurrent.getDate() < dtDOB.getDate()) {
                        args.IsValid = false;
                        return;
                    }
                }
            }
            args.IsValid = true;
        } else {
            sender.innerHTML = "Enter date in dd/MM/yyyy format ONLY."
            args.IsValid = false;
        }
    }
</script>
 
 
Screenshot
Validate Date of Birth (18+ Minimum Age validation) in ASP.Net
 
 
Browser Compatibility

The above code has been tested in the following browsers.

Internet Explorer  FireFox  Chrome  Safari  Opera 

* All browser logos displayed above are property of their respective owners.

 
 
Demo
 
 
Downloads