0

I have used the following for date input

<tr>
<td>Date of Birth :</td>
<td><input type="date" name="dob" required="required" /></td>

I want to validate the Date of birth whether it's above 18 years or not. I have no idea how to code for that.

Sri ram
  • 71
  • 2
  • 4

2 Answers2

2

You could use the min attribute, see, take into account that it might not be supported in all browsers, and that you should validate the date on server side:

<input type="date" name="dob" required="required" min="2000-02-23"/>

If you use php you can do something like this:

$today = new \DateTime();
$minDate = $today->modify('-18 years');

echo '<input type="date" name="dob" required="required" min="' . $minDate->format('Y-m-d') . '"/>'
Claudio
  • 5,078
  • 1
  • 22
  • 33
1

One of the way for client side validation is using JavaScript. You can use the below for basic validation which always can be further enhanced for more use cases.

<td><input type="date" name="dob" required="required" onchange="validateAge(this)" /></td>



<script type="text/javaScript">

function validateAge(age) {
    var input = age.value;
    if(input>=18) {
        return true;
    }
    else {
        alert("Invalid Age"+input);
        return false;
    }
}

</script>
s_u_f
  • 200
  • 1
  • 12