我想验证输入到函数的日期是否为有效日期。 我有以下HTML标记
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-calendar"></i> Check in</span>
<input id="checkinner" type="date" onchange="datechange(this)" class="form-control" data-inputmask="'alias': 'dd/mm/yyyy'" placeholder="Enter checkin date of birth">
</div>
以下是我在javascript中的功能
function datechange(element) {
if (element.value.IsValidDate())
{
//code
}
}
欣赏爱与帮助!
答案 0 :(得分:1)
试试这个:
function datechange(element) {
var date = Date.parse(element.value.toString());
if (isNaN(date))
alert('This is not a date object');
else
alert('This is a date object');
}
答案 1 :(得分:1)
试试这个,
function datechange(element) {
// Expect input as d/m/y
function isValidDate(s) {
var bits = s.split('/');
var d = new Date(bits[2], bits[1] - 1, bits[0]);
return d && (d.getMonth() + 1) == bits[1];
}
[document.getElementById("checkinner").value].forEach(function(s) {
if(isValidDate(s)){
console.log(s + ' : ' + isValidDate(s))
}else{
console.log('not valid')
}
});
}
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-calendar"></i> Check in</span>
<input id="checkinner" type="date" onchange="datechange(this)" class="form-control" data-inputmask="'alias': 'dd/mm/yyyy'" placeholder="Enter checkin date of birth">
</div>