input type data only accept data over 18 years

1

How do I put a condition inside the input of type date so it only accepts dates greater than 18 years, I was wondering if it does not have a condition to lock input for dates older than 18, it's for a form where only more than 18 can submit the form.

<label for="txtData_Nasc">Data de nascimento:</label>
<input type="date" class="form-control" id="txtData_Nasc" name="txtData_Nasc" required>
    
asked by anonymous 23.03.2017 / 18:28

2 answers

2

Elements of type <input date... accept the max attribute, however you will need to somehow calculate the minimum birth date based on the current date to assign this value, eg

Based on today's date (03/23/2017) the minimum date should be (3/23/1999), so you should put this value in the element's min attribute in AAAA-MM-DD format.

<form>
<label for="txtData_Nasc">Data de nascimento:</label>
<input type="date" class="form-control" id="txtData_Nasc" name="txtData_Nasc" max="1999-03-23" required>
<input type="submit"/>
</form>

To automatically calculate the date as php in this case:

<label for="txtData_Nasc">Data de nascimento:</label>
<input type="date" class="form-control" id="txtData_Nasc" name="txtData_Nasc" max="<?php echo date('Y-m-d', strtotime('-18 year')); ?>" required>

Example calculation date on Ideone

    
23.03.2017 / 18:51
0

You can perform validation on the client through javascript, but I recommend that besides the client perform the validation also on the server (PHP). In the input event OnBlur you can make the function call that will do the validation.

<input type="date" class="form-control" id="txtData_Nasc" name="txtData_Nasc" onblur="validaIdade()">

To create the function that validates the age you can follow the example below: Age Validation javascript

    
23.03.2017 / 19:08