How to validate if the input is a valid date in javascript
本问题已经有最佳答案,请猛点这里访问。
(P)如果给一个功能注入的日期是有效的日期,我希望能够生效。I have the following html markup(p)字母名称(P)And the following is my function in Javascript(p)字母名称(P)爱与帮助!(p)
试试这个,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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') } }); } |
1 2 | <span class="input-group-addon"><i class="fa fa-calendar"> 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"> |
试试这个:
1 2 3 4 5 6 7 | 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'); } |