【问题标题】:Validate user input dates are in given date range using javascript使用javascript验证用户输入日期是否在给定的日期范围内
【发布时间】:2013-05-04 18:53:34
【问题描述】:

我是 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


【解决方案1】:

试试这个

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;
}

【讨论】:

    【解决方案2】:

    我会这样做:

    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))            
        }
    

    【讨论】:

    • if(condition){return true;}else{return false} 总是让我感到困惑……为什么不return condition
    • @AnkitJaiswal.above fundtion 在我输入 17/05/2013 和 30/05/2013 时不起作用。你能帮我解决这个问题吗?
    • @DamithaPathmaperuma 17/05/2013 和 30/05/2013 在 24/05/2013 和 26/05/2013 之间不存在。根据问题,入住和退房时间应介于a和b之间。这不是你想要的吗?
    • @AnkitJaiswal.24/05/2013 和 26/05/2013 介于 17/05/2013 和 30/05/2013 之间。用户无法进行预订,包括 24/05/2013 和 26 /05/2013。希望你明白吗?
    【解决方案3】:

    试试这个:-

     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
     }
    

    效果很好。

    【讨论】:

      猜你喜欢
      • 2011-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-18
      • 1970-01-01
      相关资源
      最近更新 更多