使用javascript验证用户输入日期是否在给定的日期范围内

问题描述 投票:3回答:3

我是php和javascript的新手。这就是让我陷入困境的原因管理层给出了两个日期。

var a="24/05/2013";
var b="26/05/2013";

假设用户选择将日期检入为:17/05/2013并将日期检出为:30/05/2013。如您所见,这些选定日期介于上述日期之间(var a和var b)。那么如何使用JAVASCRIPT验证该场景。

需要一个支持。

提前致谢

javascript validation date date-range
3个回答
0
投票

试试这个

function dateCheck() {
    var fDate = new Date("24/05/2013");
    var lDate; = new Date("26/05/2013");
    fDate = Date.parse(document.getElementById("fDate").value);
    lDate = Date.parse(document.getElementById("lDate").value);

    if(fDate <= lDate) {
        alert("true");
        return true;
    }
    alert("false");
    return false;
}

0
投票

我会这样做的:

function dateCheck() {
    var a = new Date("24/05/2013");
    var b = new Date("26/05/2013");
    var checkinDate = Date.parse(document.getElementById("checkinDate").value);
    var checkoutDate = Date.parse(document.getElementById("checkoutDate").value);

    return((checkinDate >= a && checkinDate <= b) && 
       (checkoutDate <= b && checkoutDate >= a) && 
       (checkoutDate > checkinDate))            
}

编辑:根据OP的说明

function dateCheck() {
        var a = new Date("24/05/2013");
        var b = new Date("26/05/2013");
        var checkinDate = Date.parse(document.getElementById("checkinDate").value);
        var checkoutDate = Date.parse(document.getElementById("checkoutDate").value);

        return(( a > checkinDate && a < checkoutDate) && 
           (b < checkoutDate && b > checkinDate) && 
           (checkoutDate > checkinDate))            
    }

0
投票

试试这个 :-

 var fdt= new Date("20/02/2013");
    var tdt = new Date("10/05/2013");

function validateFromAndToDate(fdt,tdt){

    var dt1  = dte1.value.substring(0,2); 
    var mon1 = dte1.value.substring(3,5); 
    var yr1  =dte1.value.substring(6,10); 

    var dt2  = dte2.value.substring(0,2); 
    var mon2 = dte2.value.substring(3,5); 
    var yr2  = dte2.value.substring(6,10); 
    var date1 = new Date(yr1, mon1-1, dt1); 
    var date2 = new Date(yr2, mon2-1, dt2); 

        if (date2<date1){
            alert("Date period must be within the given date!");
            return false
        }
    return true
 }

它工作正常。

© www.soinside.com 2019 - 2024. All rights reserved.