Age Validation from Birthdate using DatePicker
Posted: Wed Feb 15, 2012 8:23 pm
Hi, I've successfully integrated the date-picker so users can choose their birth-date, and I'd like to add a validation to check if the user is older than 18. I think I first should use some javascript to calculate the age, which I have achieved with the code below, but I am not sure how to integrate this in a php validation. I was thinking a could have a new text field ["Your Age" ] next to the birthdate field so that when the user chooses the birthdate, this new field "Your Age: " will show the result from the javascript calculation. And then using that field info we could validate the age.If this is possible, I'd really appreciate your help.
Code: Select all
<script type="text/javascript"><!--
$(document).ready(function() {
$('#birthdate').datepicker({changeMonth: true, maxDate: '0',changeYear: true, yearRange: '1920:1994', dateFormat: 'dd-mm-yy',
onClose: function(){
var today = new Date(),
birthday = $('#birthdate').datepicker("getDate"),
age = (
(today.getMonth() > birthday.getMonth())
||
(today.getMonth() == birthday.getMonth() && today.getDate() >= birthday.getDate())
) ? today.getFullYear() - birthday.getFullYear() : today.getFullYear() - birthday.getFullYear()-1;
alert("Age: " + age);}
});
});
//--></script>