我有一个表格,我只想通过它接受给定日期范围内的日期。但是,无论我获取哪个日期,它都会返回 false 并显示不正确的范围。
以下是相关代码 -
注册.html-
<div class="form-group row">
<label for="date" class="col-sm-2 col-form-label class='control-label' ">Date of Birth</label>
<div class="col-sm-5">
<input type="date" max="2010-12-31" min="1990-01-01" id="dob" placeholder="Date of birth">
</div>
</div>
控制器.js-
var dobInput = document.forms["myform"]["dob"].value;
var maxdate = new Date("2010-12-31");
var mindate= new Date("1990-01-01");
if(maxdate>dobInput && dobInput>mindate)
{
alert("correct range");
}
else{
alert("Input out of range");
return false;
}
您获得的值作为字符串返回,但是您尝试针对日期类型变量对其进行评估 - 这将不起作用,因为将完成字符串到字符串的评估按字典顺序 https://stackoverflow.com/questions/7087811/why-is-one-string-greater-than-the-other-when-comparing-strings-in-javascript.
您应该首先将值转换为日期:
var inputDate = new Date(dobInput);
if(maxdate>inputDate && inputDate >mindate){
...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)