我是javascript中的首发:
我需要一个检测用户是否输入日期的功能..如果日期小于今天日期,则提醒他...
HTML:
<input type="date" name="StartDate" id="userdate" required />
我尝试过的JavaScript(当然没有工作):
function TDate() {
var UserDate = document.getElementById("userdate").value;
var ToDate = Date.now();
if (UserDate != ToDate) {
alert("The Date must be Bigger or Equal to today date")
return false;
}
return true;}
答案 0 :(得分:2)
我创造了一个小提琴
https://jsbin.com/jidogo/edit?html,js,output
<input type="date" name="StartDate" id="userdate" onchange="TDate()" required />
JS
function TDate() {
var UserDate = document.getElementById("userdate").value;
var ToDate = new Date();
if (new Date(UserDate).getTime() <= ToDate.getTime()) {
alert("The Date must be Bigger or Equal to today date");
return false;
}
return true;
}
答案 1 :(得分:0)
我假设您可以通过var selectedDate = document.getElementById("userdate").value
获取所选的Date对象。然后你可以比较两个日期,比如比较2整数:
function TDate() {
var selectedDate = document.getElementById("userdate").value;
if (selectedDate < Date.now()) {
alert("The Date must be Bigger or Equal to today date")
return false;
}
return true;
}
答案 2 :(得分:0)
由于Juank:
,我认为这比其他答案更清晰var now = new Date(),
minDate = now.toISOString().substring(0,10);
$('#my-date-input').prop('min', minDate);