Thursday 25 July 2013

How to validate two dates difference in JavaScript


In this article I will explain how to validate two dates difference in JavaScript.

function CompareDateTest(txtfrom, txtto) {


        var From = document.getElementById(txtfrom).value;
        var To = document.getElementById(txtto).value;
      
        var POdmy = From.split("/");
        var IVdmy = To.split("/");


        if (parseInt(POdmy[2], 10) > parseInt(IVdmy[2], 10)) {
            alert("Expiration Date must be greater than Effective Date .");
            return false;
        }
        else if (parseInt(POdmy[0], 10) > parseInt(IVdmy[0], 10) && (parseInt(POdmy[2], 10) == parseInt(IVdmy[2], 10))) {
            alert("Expiration Date must be greater than Effective Date .");
            return false;
        }

        else if (parseInt(POdmy[1], 10) > parseInt(IVdmy[1], 10) && parseInt(POdmy[0], 10) == parseInt(IVdmy[0], 10) && parseInt(POdmy[2], 10) == parseInt(IVdmy[2], 10)) {
            alert("Expiration Date must be greater than Effective Date .");
            return false;
        }     
    
        else if (From == "") {
            alert("Effective Date is mandatory .");
            return false;
        }
        return true;
    }

No comments:

Post a Comment