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

Validate user input dates are in given date range using javascript

本文关键字:日期 范围内 是否 验证 用户 输入 使用 javascript      更新时间:2023-09-26

我是php和javascript的新手。这就是让我陷入麻烦的原因行政部门给出了两个日期。

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

假设用户选择check in Date为:17/05/2013, check out Date为:30/05/2013。如您所见,这些选定的日期位于上述日期(变量a和变量b)之间。那么我如何使用JAVASCRIPT验证这个场景呢?

需要支持。

thanks in advance

try this

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

我将这样做:

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

试试这个:-

 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
 }