Check given Date is Greater than Today using JavaScript

Last Reply 7 months ago By dharmendr

Posted 7 months ago

Hi,

I have wrote code for date and i want to check that date with curent date. if date is greater than current date then it should give a msg.

on clicking the submit button it has to check before going to server

<script type="text/javascript">
    var isShift = false;
    var seperator = "/";
    window.onload = function () {
        //Reference the Table.
        var tblForm = document.getElementById("masterDataFeedDate_Id");
 
        //Reference all INPUT elements in the Table.
        var inputs = document.getElementsByTagName("input");
 
        //Loop through all INPUT elements.
        for (var i = 0; i < inputs.length; i++) {
            //Check whether the INPUT element is TextBox.
            if (inputs[i].type == "text") {
                //Check whether Date Format Validation is required.
                if (inputs[i].className.indexOf("date-format") != 1) {
                       
                    //Set Max Length.
                    inputs[i].setAttribute("maxlength", 10);
 
                    //Only allow Numeric Keys.
                    inputs[i].onkeydown = function (e) {
                        return IsNumeric(this, e.keyCode);
                    };
 
                    //Validate Date as User types.
                    inputs[i].onkeyup = function (e) {
                        ValidateDateFormat(this, e.keyCode);
                    };
                }
            }
        }
    };
 
    function IsNumeric(input, keyCode) {
        if (keyCode == 16) {
            isShift = true;
        }
        //Allow only Numeric Keys.
        if (((keyCode >= 48 && keyCode <= 57) || keyCode == 8 || keyCode <= 37 || keyCode <= 39 || (keyCode >= 96 && keyCode <= 105)) && isShift == false) {
            if ((input.value.length == 4 || input.value.length == 7) && keyCode != 8) {
                input.value += seperator;
            }
 
            return true;
        }
        else {
            return false;
        }
    };
 
    function ValidateDateFormat(input, keyCode) {
        var dateString = input.value;
        if (keyCode == 16) {
            isShift = false;
        }
        var regex = /^(19[5-9][0-9]|20[0-4][0-9]|2050)[-/](0?[1-9]|1[0-2])[-/](0?[1-9]|[12][0-9]|3[01])$/;

        
 
        //Check whether valid dd/MM/yyyy Date Format.
        if (regex.test(dateString) || dateString.length == 0) {
            ShowHideError(input, "none");
        } else {
            ShowHideError(input, "block");
        }
    };

    
    function ShowHideError(textbox, display) {
        var row = textbox.parentNode.parentNode;
        var errorMsg = row.getElementsByTagName("span")[0];
        if (errorMsg != null) {
            errorMsg.style.display = display;
        }
    };



    function checkdate() {
        var dateCheck = document.getElementById('masterDataFeedDate_Id').value;
        var myDate = new Date(dateCheck);
        var today = new Date();
        if ( myDate > today ) { 
             alert("The date is wrong.");
            return false;
        }
        return true;
    }
</script>

 

You are viewing reply posted by: dharmendr 7 months ago.
Posted 7 months ago

Hi gouthamgou,

Refer below code.

HTML

<span>Invalid Format</span>
<input type="text" id="masterDataFeedDate_Id" maxlength="10" />
<input type="button" name="" value="Validate" onclick="Validate()" />
<script type="text/javascript">
    var isShift = false;
    var seperator = "/";
    window.onload = function () {
        //Reference the Table.
        var tblForm = document.getElementById("masterDataFeedDate_Id");
  
        //Reference all INPUT elements in the Table.
        var inputs = document.getElementsByTagName("input");
  
        //Loop through all INPUT elements.
        for (var i = 0; i < inputs.length; i++) {
            //Check whether the INPUT element is TextBox.
            if (inputs[i].type == "text") {
                //Check whether Date Format Validation is required.
                if (inputs[i].className.indexOf("date-format") != 1) {
                        
                    //Set Max Length.
                    inputs[i].setAttribute("maxlength", 10);
  
                    //Only allow Numeric Keys.
                    inputs[i].onkeydown = function (e) {
                        return IsNumeric(this, e.keyCode);
                    };
  
                    //Validate Date as User types.
                    inputs[i].onkeyup = function (e) {
                        ValidateDateFormat(this, e.keyCode);
                    };
                }
            }
        }
    };
  
    function IsNumeric(input, keyCode) {
        if (keyCode == 16) {
            isShift = true;
        }
        //Allow only Numeric Keys.
        if (((keyCode >= 48 && keyCode <= 57) || keyCode == 8 || keyCode <= 37 || keyCode <= 39 || (keyCode >= 96 && keyCode <= 105)) && isShift == false) {
            if ((input.value.length == 4 || input.value.length == 7) && keyCode != 8) {
                input.value += seperator;
            }
  
            return true;
        }
        else {
            return false;
        }
    };
  
    function ValidateDateFormat(input, keyCode) {
        var dateString = input.value;
        if (keyCode == 16) {
            isShift = false;
        }
        var regex = /^(19[5-9][0-9]|20[0-4][0-9]|2050)[-/](0?[1-9]|1[0-2])[-/](0?[1-9]|[12][0-9]|3[01])$/;          
  
        //Check whether valid dd/MM/yyyy Date Format.
        if (regex.test(dateString) || dateString.length == 0) {
            ShowHideError(input, "none");
        } else {
            ShowHideError(input, "block");
        }
    };
 
     
    function ShowHideError(textbox, display) {
        var row = textbox.parentNode.parentNode;
        var errorMsg = document.getElementsByTagName("span")[0];
        if (errorMsg != null) {
            errorMsg.style.display = display;
        }
    };

        function Validate() {
        //Get the date from the TextBox.
        var dateString = document.getElementById("masterDataFeedDate_Id").value;
        var dtCurrent = new Date();
        var parts = dateString.split("/");
        var dt = new Date(parts[1] + "/" + parts[2] + "/" + parts[0]);
        if(dt > dtCurrent){
            alert('Given date is greater than the current date.');
        }
    };
</script>

Demo