[Solved] Date validation working in Chrome but not in Safari using JavaScript and jQuery

098612
 
on Sep 03, 2021 07:20 AM
778 Views

javascript date validation working in chrome but not in safari

For reference i have sent code, this code is working in chrome browser but not in safari.

how to do this? please help me.

function isToday(dateParameter) {
    var today = new Date();
    return dateParameter.getDate() === today.getDate() && dateParameter.getMonth() === today.getMonth() && dateParameter.getFullYear() === today.getFullYear();
}

function checkMonth(dateString) {
    var enteredMS = new Date(dateString).getTime();
    var currentMS = new Date().getTime();
    var twoMonthMS = new Date(new Date().setMonth(new Date().getMonth() + 2)).getTime();
    if (enteredMS >= twoMonthMS) {
        return true;
    }
    return false;
}

function checkDate(day, month, year) {
    //alert(browserName)
    var regd = new RegExp("^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})\$");
    var date = month + "/" + day + "/" + year;
    var date = new Date(date);
    var today = new Date();
    var vdob = regd.test(date);
    var err_txt = "";
    //alert(browserName)
    var sta = isToday(new Date(date));
    if (sta === true) {
        return true;
    }

    var futdt = checkMonth(date);
    if (futdt === true) {
        err_txt += ("Please select a before two months Date.\n")
        $("#selectdate").next('.validation').hide();
        $("#selectdate").after("<div class='validation' style='color:red;margin-bottom: 20px;text-align:center;'><b>Policy start date cannot be more than 60 days</b></div>");
        e.preventDefault();
    }

    if (date.getDate() != day || (date.getTime() < today.getTime())) {
        err_txt += ("Please select a valid Date.\n")
        $("#selectdate").next('.validation').hide();
        $("#selectdate").after("<div class='validation' style='color:red;margin-bottom: 20px;text-align:center;'><b>Please select a valid Date</b></div>");
        e.preventDefault();
    }

    return (err_txt);
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Sep 07, 2021 02:51 AM

Refer below code checked in all browser.

JavaScript

$(function () {
    $(".next").click(function (e) {
        if ($('#ddlday').val() == '' && $('#ddlmonth').val() == '' && $('#ddlyear').val() == '') {
            $("#selectdate").next('.validation').hide();
            $("#selectdate").after("<div class='validation' style='color:red;margin-bottom: 20px;text-align:center;'><b>Please select date</b></div>");
            e.preventDefault();
        }
        else {
            $("#selectdate").next('.validation').hide();
            isValid = true;
        }

        if ($('#ddlday').val() == '') {
            $("#ddlday").next('.validation').hide();
            $("#ddlday").after("<div class='validation' style='color:red;margin-bottom: 20px;text-align:center;'>*</div>");
            e.preventDefault();

        } else {
            $("#ddlday").next('.validation').hide();
            isValid = true;
        }
        if ($('#ddlmonth').val() == '') {
            $("#ddlmonth").next('.validation').hide();
            $("#ddlmonth").after("<div class='validation' style='color:red;margin-bottom: 20px;text-align:center;'>*</div>");
            e.preventDefault();

        } else {
            $("#ddlmonth").next('.validation').hide();
            isValid = true;
        }
        if ($('#ddlyear').val() == '') {
            $("#ddlyear").next('.validation').hide();
            $("#ddlyear").after("<div class='validation' style='color:red;margin-bottom: 20px;text-align:center;'>*</div>");
            e.preventDefault();

        } else {
            $("#ddlyear").next('.validation').hide();
            isValid = true;
        }
        var ddlday = document.getElementById("ddlday");
        var ddlmonth = document.getElementById("ddlmonth");
        var ddlyear = document.getElementById("ddlyear");
        var strddlday = ddlday.value;
        var strddlmonth = ddlmonth.value;
        var strddlyear = ddlyear.value;
        checkDate(strddlday, strddlmonth, strddlyear, e)

        current_fs = $(this).parent();
        next_fs = $(this).parent().next();

        //Add Class Active
        $("#progressbar li").eq($("fieldset").index(next_fs)).addClass("active");

        //show the next fieldset
        next_fs.show();

        //hide the current fieldset with style
        current_fs.animate({ opacity: 0 }, {
            step: function (now) {
                // for making fielset appear animation
                opacity = 1 - now;

                current_fs.css({
                    'display': 'none',
                    'position': 'relative'
                });
                next_fs.css({ 'opacity': opacity });
            },
            duration: 500
        });
        setProgressBar(++current);
    });
});

function isToday(dateParameter) {
    var today = new Date();
    return dateParameter.getDate() === today.getDate() && dateParameter.getMonth() === today.getMonth() && dateParameter.getFullYear() === today.getFullYear();
}

function checkMonth(dateString) {
    var enteredMS = new Date(dateString);
    var twoMonthMS = new Date(new Date().setMonth(new Date().getMonth() + 2));
    if (enteredMS >= twoMonthMS) {
        return true;
    }
    return false;
}

function getMonthFromString(mon) {
    var d = Date.parse(mon + "1, 2012");
    if (!isNaN(d)) {
        return new Date(d).getMonth() + 1;
    }
    return -1;
}

function checkDate(day, month, year, e) {
    var date = getMonthFromString(month) + "/" + day + "/" + year;
    var date = new Date(date);
    var today = new Date();

    var sta = isToday(date);
    if (sta === true) {
        return true;
    }

    var err_txt = "";
    var futdt = checkMonth(date);
    if (futdt === true) {
        err_txt += ("Please select a before two months Date.\n")
        $("#selectdate").next('.validation').hide();
        $("#selectdate").after("<div class='validation' style='color: red; margin-bottom: 20px; text-align: center;'><b>Policy start date cannot be more than 60 days</b></div>");
        e.preventDefault();
    }

    if (date < today) {
        err_txt += ("Please select a valid Date.\n")
        $("#selectdate").next('.validation').hide();
        $("#selectdate").after("<div class='validation' style='color: red; margin-bottom: 20px; text-align: center;'><b>Please select a valid Date</b></div>");
        e.preventDefault();
    }

    return (err_txt);
}