JS实现对Date Range的认证

JS实现对Date Range的认证。

认证条件如下:

1):startDate 与 endDate 都是Date格式

2):startDate <= endDate

3):endDate <= today  (当然可以根据具体要求,具体分析)

function ValidateDateRange(startDateText, endDateText) {
        var flag = false;
        if (isDate(startDateText) && isDate(endDateText)) {
            var startDate = new Date(startDateText);
            var endDate = new Date(endDateText);
            var now = new Date();
            if (startDate <= endDate && endDate <= now) {
                flag = true;
            }
        }
        return flag;
    }

    function isDate(txtDate) {
        var currVal = txtDate;
        if (currVal == '')
            return false;

        //Declare Regex  
        var rxDatePattern = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
        var dtArray = currVal.match(rxDatePattern); // is format OK?

        if (dtArray == null)
            return false;

        //Checks for dd/mm/yyyy format.
        var dtDay = dtArray[1];
        var dtMonth = dtArray[3];
        var dtYear = dtArray[5];

        if (dtMonth < 1 || dtMonth > 12)
            return false;
        else if (dtDay < 1 || dtDay > 31)
            return false;
        else if ((dtMonth == 4 || dtMonth == 6 || dtMonth == 9 || dtMonth == 11) && dtDay == 31)
            return false;
        else if (dtMonth == 2) {
            var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0));
            if (dtDay > 29 || (dtDay == 29 && !isleap))
                return false;
        }

        return true;
    }

....

posted @ 2013-06-10 14:05  Eric Sun  阅读(1066)  评论(0编辑  收藏  举报