Extract datepicker.js and datepicker.css from the archive downloadable from above link. Create index.php (Containing Form) & birthchecker.js.
index.php
jQuery Birthday Form Field & Checker
jQuery Birthday Form Field & Checker
birthchecker.js
$(document).ready(function(){
v=$(‘#birth’);
function above13(v){
cur=new Date();
cls=new Date(v.val());
age = new Date(cur – cls).getFullYear() – 1970;
console.log(age);
if(age<13){
v.css("background","red");
$("#ermsg").show();
}else{
v.css("background","white");
$("#ermsg").hide();
}
}
v.DatePicker({
format:’Y-m-d’,
date: v.val(),
current: v.val(),
starts: 1,
position: ‘r’,
onBeforeShow:function(){
v.DatePickerSetDate(v.val(), true);
},
onChange:function(formated, dates){
v.val(formated);
v.DatePickerHide();
above13(v);
}
});
v.on("keyup",function(){
above13(v);
});
});
submit.php
function age($birthday){
list($day,$month,$year) = explode("/",$birthday);
$year_diff = date("Y") – $year;
$month_diff = date("m") – $month;
$day_diff = date("d") – $day;
if ($day_diff < 0 && $month_diff==0){$year_diff–;}
if ($day_diff < 0 && $month_diff < 0){$year_diff–;}
return $year_diff;
/* http://www.subinsb.com/2013/05/the-best-age-calculation-code-in-php.html */
}
$birth=$_POST[‘birth’];
if(isset($_POST) && $birth!="){
$birth=explode("-",$birth);
$nbir=$birth[2]."/".$birth[1]."/".$birth[0];
$age=age($nbir);
echo "Your Age is $age.
Click Here To Go To Demo Form";
}
?>
That’s all. If you have any problems / suggestions / feedback, comment it. I am here 10/7.