Validate entered Month should not be less than specific month from Current Date in JavaScript

Last Reply 4 months ago By pandeyism

Posted 4 months ago

hello,

i am using this javascript to validate age it works  

i want to modify it so i can check months from current date to texbox date

textbox date should be greater than 6 months

    <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>

 

<asp:CustomValidator ID="CustomValidator2" runat="server" ClientValidationFunction="ValidateDOB"
    ControlToValidate="TextBox16" ErrorMessage="" ForeColor = "Red" />
<asp:TextBox ID="TextBox16" runat="server"></asp:TextBox>

 

You are viewing reply posted by: pandeyism 4 months ago.
Posted 4 months ago

Hey

Please refer below sample.

HTML

<script type="text/javascript">
    function ValidateDOB(sender, args) {
        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))$/;
        if (regex.test(dateString)) {
            var parts = dateString.split("/");
            var dt = new Date(parts[1] + "/" + parts[0] + "/" + parts[2]);
            var dtCurrent = new Date();
            if (dt.getYear() == dtCurrent.getYear()) {
                if (dt.getMonth() > dtCurrent.getMonth() - 6) {
                    alert('month should not be less than 6 months.');
                }
            }
            args.IsValid = true;
        } else {
            sender.innerHTML = "Enter date in dd/MM/yyyy format ONLY."
            args.IsValid = false;
        }
    }
</script>
<div>
    <asp:CustomValidator ID="CustomValidator2" runat="server" ClientValidationFunction="ValidateDOB"
        ControlToValidate="TextBox16" ErrorMessage="" ForeColor="Red" />
    <asp:TextBox ID="TextBox16" runat="server"></asp:TextBox>
</div>

Screenshot