Wednesday, 2 April 2014

How to check From date , To date using jquery in generic format


    function IsDateNull(dateValue) {
        if (dateValue == null || dateValue == "") {
            return false;
        }
        else {
            return true;
        }
    }

    function IsValidDate(validDate) {
        var validformat = /^\d{1,2}\/\d{1,2}\/\d{4}$/; //Basic check for format validity
        var a = validDate.match(validformat);
        if (a == null)
            return false;
        var item = [];
        item = a[0].split('/');
        if (item[0] < 1 || item[0] > 12)
            return false;
        else if (item[1] < 1 || item[1] > 31)
            return false;
        else if ((item[0] == 4 || item[0] == 6 || item[0] == 9 || item[0] == 11) && item[1] == 31)
            return false;
        else if (item[0] == 2) {
            var isleap = (item[2] % 4 == 0 && (item[2] % 100 != 0 || item[2] % 400 == 0));
            if (item[1] > 29 || (item[1] == 29 && !isleap))
                return false;
        }

    }

    function IsValidDateRange(validDateRange) {//Detailed check for valid date ranges
        var monthfield = validDateRange.split("/")[0]
        var dayfield = validDateRange.split("/")[1]
        var yearfield = validDateRange.split("/")[2]
        var dayobj = new Date(yearfield, monthfield - 1, dayfield)
        if ((dayobj.getMonth() + 1 != monthfield) || (dayobj.getDate() != dayfield) || (yearfield < 1753 || yearfield > 9999)) {
            return false;
        }
        else {
            return true;
        }
    }

    function DateAlert(DateType) {
        return "Please Enter or Select a Valid " + DateType + " Date(Ex:MM/DD/YYYY).";
    }

    function ValidDateAlert(DateType) {
        return "Please Enter or Select a Valid " + DateType + " Date(Ex:MM/DD/YYYY). And year should be greater than equal to 1753 and less than equal to 9999.";
    }

    function InValidDateAlert(DateType) {
        return "Invalid " + DateType + " Date";
    }

    function CheckDates(FromID, ToID) {
        var IsfromDate = true;
        var FromDate = $(FromID).val();
        var IsToDate = true;
        var ToDate = $(ToID).val();
        var fromDateObj = new Date(FromDate);
        var toDateObj = new Date(ToDate);
        if (IsDateNull(FromDate) == false) {
            IsfromDate = false;
            alert(DateAlert("From"));
            $(FromID).focus();
            return false;
        }
        else {
            if (IsValidDate(FromDate) == false) {
                IsfromDate = false;
                alert(InValidDateAlert("From"));
                $(FromID).focus();
                return false;
            }
            else {
                if (IsValidDateRange(FromDate) == false) {
                    IsfromDate = false;
                    alert(ValidDateAlert("From"));
                    $(FromID).focus();
                    return false;
                }
            }
        }
        if (IsfromDate == true) {
            if (IsDateNull(ToDate) == false) {
                IsToDate = false;
                alert(DateAlert("To"));
                $(ToID).focus();
                return false;
            }
            else {
                if (IsValidDate(ToDate) == false) {
                    IsToDate = false;
                    alert(InValidDateAlert("To"));
                    $(ToID).focus();
                    return false;
                }
                else {
                    if (IsValidDateRange(ToDate) == false) {
                        IsToDate = false;
                        alert(ValidDateAlert("To"));
                        $(ToID).focus();
                        return false;
                    }
                }
            }
            if (IsfromDate == true && IsToDate == true) {
                if (fromDateObj > toDateObj) {
                    alert("Please ensure that the From Date is greater than or equal to the To Date.");
                    $(FromID).focus();
                    return false;
                }
                else {
                    return true;
                }
            }
        }
    }

No comments:

Post a Comment