Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery验证日期字段必须为future_Jquery - Fatal编程技术网

jquery验证日期字段必须为future

jquery验证日期字段必须为future,jquery,Jquery,我有办法 $.validator.addMethod("dateDDMMYYYY", function(value, element) { return value.match(/^(0?[1-9]|[12][0-9]|3[0-2])[.,/ -](0?[1-9]|1[0-2])[.,/ -](19|20)?\d{2}$/); }, "* Please enter a valid date (dd/mm/yyyy)" ); 检查输入的日期是否为有效的dd

我有办法

$.validator.addMethod("dateDDMMYYYY",
    function(value, element) {
        return value.match(/^(0?[1-9]|[12][0-9]|3[0-2])[.,/ -](0?[1-9]|1[0-2])[.,/ -](19|20)?\d{2}$/);
    },
    "* Please enter a valid date (dd/mm/yyyy)"
);
检查输入的日期是否为有效的dd/mm/yyyy日期,但我想修改(或创建一个新方法)以检查输入的日期是否在未来,并检查未来的距离

例如,如果输入的日期在未来少于2天,我希望显示消息,但如果日期在未来大于2天,则不显示消息


提前感谢

以毫秒为单位的两天持续时间为:

var twoDays = 2 * 24 * 60 * 60 * 1000
因此,您可以在未来两天内创建一个日期,如下所示:

var now = new Date()
var future = new Date(now.getTime() + twoDays)
Javascript还有一个日期构造函数,如下所示:

new Date(year, month [, day, hour, minute, second, millisecond]);
因此,只需使用正则表达式提取输入的日期(我会让您这样做)

那么你的测试是:

entered - now > twoDays

如果该表达式为真,则表示他们输入了一个未来两天以上的日期。

一旦确定该日期为有效日期,您可以对其进行分析,并将其与当前日期进行比较:

// Assuming value is the date string.
var date = new Date(value);

// Create a new date, stripping the time away.
var today = new Date(new Date().toDateString());

// Subtracting one date from another gives you the number
//  of milliseconds between the two. Divide that down to days.
var daysInTheFuture = (date - today) / 1000 / 60 / 60 / 24;

嗨,这似乎是最有效的方法。我还必须实现date.js作为我的今天日期,并且输入的日期在一起似乎不是很好。但现在它起作用了。谢谢
// Assuming value is the date string.
var date = new Date(value);

// Create a new date, stripping the time away.
var today = new Date(new Date().toDateString());

// Subtracting one date from another gives you the number
//  of milliseconds between the two. Divide that down to days.
var daysInTheFuture = (date - today) / 1000 / 60 / 60 / 24;