簡體   English   中英

使用javascript驗證用戶輸入日期是否在給定的日期范圍內

[英]Validate user input dates are in given date range using javascript

我是 php 和 javascript 的新手。 這就是讓我陷入困境的原因 管理部門給出了兩個日期。

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

假設如果用戶在日期為選擇檢查: 17/05/2013和退房日期: 30/05/2013 如您所見,這些選定的日期介於上述日期之間(var a 和 var b)。 那么我如何使用 JAVASCRIPT 驗證該場景。

需要這方面的支持。

提前致謝

試試這個

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
 }

它工作正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM