Post by marvmen21 » 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>

You want to thank me for my time! :) Click here to donate


Active Member

Posts

Joined
Tue Nov 09, 2010 4:54 am

Post by celestial » Wed Feb 15, 2012 10:29 pm


Celestial - Martín Abel Rosales
WhatsApp: 50671482211
Email: martinrosales2012@hotmail.com
Skype: martin.abel.rosales
San José , Costa Rica


User avatar
Expert Member

Posts

Joined
Sat Mar 20, 2010 4:19 am
Location - Costa Rica

Post by celestial » Wed Feb 15, 2012 10:31 pm

<!DOCTYPE html>
<html>
<head>
<link class="jsbin" href="http://ajax.googleapis.com/ajax/libs/jq ... ery-ui.css" rel="stylesheet" type="text/css" />
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jq ... "></script>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jq ... "></script>
<meta charset=utf-8 />
<title>JS Bin</title>

</head>
<body>
Birthday: <input type="text" class="datepicker" id="user_Birthday" />
<br />
<button>Get Age</button>
<script>
$(function(){
$('#user_Birthday').datepicker({changeYear: true, changeMonth: true});
$('button').click(function(){
var today = new Date(),
birthday_val = $("#user_Birthday").val().split('/'),
birthday = new Date(birthday_val[2],birthday_val[0]-1,birthday_val[1]),
todayYear = today.getFullYear(),
todayMonth = today.getMonth(),
todayDay = today.getDate(),
birthdayYear = birthday.getFullYear(),
birthdayMonth = birthday.getMonth(),
birthdayDay = birthday.getDate(),
yearDifference = (todayMonth == birthdayMonth && todayDay > birthdayDay) ?
todayYear - birthdayYear : (todayMonth > birthdayMonth) ? todayYear - birthdayYear : todayYear - birthdayYear-1;

alert("Age: " + yearDifference);
});
})

</script>

Celestial - Martín Abel Rosales
WhatsApp: 50671482211
Email: martinrosales2012@hotmail.com
Skype: martin.abel.rosales
San José , Costa Rica


User avatar
Expert Member

Posts

Joined
Sat Mar 20, 2010 4:19 am
Location - Costa Rica
Who is online

Users browsing this forum: Google [Bot], Semrush [Bot] and 83 guests