如何使用HTML5日期输入验证日期范围?

时间:2016-03-08 17:33:13

标签: javascript html

当找到航班时,会调用TwoFunctions()函数。单击HTML中的按钮。然后它将调用剩下的两个函数。

第一个Format()用于检查输入的格式是否正确以及是否已输入日期。此功能似乎工作正常,并正确验证/无效。

但是第二个,validDate()似乎无法使将来三个月的日期无效,或者到达时间是在出发时间之前,并且想知道为什么?

我已经使用警报来检查是否正在调用该函数,但它仍然声明日期已经过验证'什么时候不应该。

var departuredate = document.getElementById("departdate").value; 
var arrivaldate = document.getElementById("arrivedate").value;

var months3 = 90 * 24 * 60 * 60 * 1000; //3 months 
var departuserspecifiedTime = departuredate.getTime();
var arrivaluserspecifiedTime = arrivaldate.getTime();  
var currentTime = CurrentDate.getTime(); //current time and date
var departdifference = departuserspecifiedTime - currentTime; //difference between departure time and the current time
var arrivaldifference = arrivaluserspecifiedTime - currentTime; //difference between arrival time and the current time

function TwoFunctions() {
    departuredate = document.getElementById("departdate").value;
    arrivaldate = document.getElementById("arrivedate").value;
    //alert(departuredate);
    //alert(arrivaldate);

    var result = Format();
    var result1 = validDate();

    if (result == true) {
        result1;
    } else {
        alert("Error 123");
    }
    return result
}

function validDate() {
    alert('debug');
    if (departdifference <= (1000 * 60 * 60)) //if the departure time is in the past or within an hour of the current time, it is invalidated as it is too soon
    {
        document.getElementById("temp").innerHTML = "Date selected is in the past";
        document.MyForm.departdate.focus();
        document.getElementById("departdate").style.border = '1px solid red';
        return false;
    } else if (arrivaldifference <= (1000 * 60 * 60)) //if the arrival time is in the past or within an hour of the current time, it is invalidated as it is too soon
    {
        document.getElementById("temp").innerHTML = "Date selected is in the past";
        document.MyForm.arrivedate.focus();
        document.getElementById("arrivedate").style.border = '1px solid red';
        return false;
    } else if (departdifference && arrivaldifference >= months3) //if the departure/arrival date is over 3 months away from todays date it is invalidated
    {
        document.getElementById("temp").innerHTML = "Only 3 months          advance booking is allowed";
        document.getElementById("departdate").style.border = '1px solid red';
        document.getElementById("arrivedate").style.border = '1px solid red';
        return false;
    } else if (arrivaluserspecifiedTime < departuserspecifiedTime) // if the arrival date is before the departure date it is invalidated 
    {
        alert("Arrival date selected is before the departure date");
        document.getElementById("departdate").focus();
        document.getElementById("arrivedate").focus();
        document.getElementById("departdate").style.border = '1px solid red';
        document.getElementById("arrivedate").style.border = '1px solid red';
        return false;
    } else {
        alert('Dates are validated');
        return true;
    }
}

基本上我想知道为什么上面的函数使所有条目无效并且没有像我希望代码那样使它们失效。

0 个答案:

没有答案