Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
137
rated 0 times [  138] [ 1]  / answers: 1 / hits: 50131  / 7 Years ago, fri, march 10, 2017, 12:00:00

How to validate html <input type='date'/> is greater than today?



The following is my code:



<div class=form-group>
<label class=col-sm-2 for='id_dateOfBirth'>Birth Date </label>
<div class=col-sm-10>
<input class='form-control' name=dateOfBirth id='id_dateOfBirth' type='date'>
</div>




I want to check whether date is greater than today using JavaScript.


More From » jquery

 Answers
5

What I understand from your question is - Before posting the form, you'd like to check if the DOB field has a date that must not be greater than today's date.. If this is correct, then try this, Hope this help -



<script type=text/javascript>
function checkDOB() {
var dateString = document.getElementById('id_dateOfBirth').value;
var myDate = new Date(dateString);
var today = new Date();
if ( myDate > today ) {
$('#id_dateOfBirth').after('<p>You cannot enter a date in the future!.</p>');
return false;
}
return true;
}
</script>

[#58599] Wednesday, March 8, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
janjadonb

Total Points: 4
Total Questions: 114
Total Answers: 118

Location: Mali
Member since Fri, Dec 3, 2021
3 Years ago
janjadonb questions
;