...

View Full Version : 2 questions involving dates in a form



Soopamn36
06-30-2004, 03:29 PM
I have a reservation form that I am developing for a club...
how do I a) set the reservation field so that the input type and drop down menus are set for today's date(like on orbits.com)

and b)how do I set up the date of birth field so that it can validate that the client is at least 21 years of age?

sad69
06-30-2004, 07:56 PM
Well it looks like orbitz.com is using a server-side language (like PHP or ASP) to generate their pages.

So what you might have is the following:


$months = array();
$months[0] = 'Jan';
$months[1] = 'Feb';
...

/*
* if month=Jan, output <option value="0" selected>Jan</option>
* else, <option value="0">Jan</option>
*/
echo '<select name="month">';
foreach($months as $i=>$month) {
echo '<option value="'.$month.'"';
if(date('M') == $month) //select today's month
echo ' selected';
echo '>'.$i.'</option>';
}
echo '</select>';

//days
echo '<select name="day">';
for($i = 1; $i <= 31; $i++) {
echo '<option value="'.$i.'"';
if(date('j')*1 == $i) //select today's day
echo ' selected';
echo '>'.$i.'</option>';
}
echo '</select>';


As far as validating their age, it depends if you want the age validated before submit or after submit. If before, you'd use Javascript; if after, you'd use a server-side language like PHP or ASP.

Basically you have to put the month, day, and year together and create a date.

PHP:


function myAge($m, $d, $y) {
$dob_m = $m;
$dob_d = $d;
$dob_y = $y;

$tdy_m = date('n');
$tdy_d = date('j');
$tdy_y = date('Y');

$diff_y = $tdy_y - $dob_y;
$diff_m = $tdy_m - $dob_m;
$diff_d = $tdy_d - $dob_d;

if(($diff_m < 0) || ($diff_m == 0 && $diff_d < 0)) {
$diff_y -= 1;
}

return $diff_y;
}


Javascript:


function myAge(m, d, y) {
var dob_m = m;
var dob_d = d;
var dob_y = y;

var tdy = new Date();
var tdy_m = tdy.getMonth();
var tdy_d = tdy.getDate();
var tdy_y = tdy.getFullYear();

var diff_y = tdy_y - dob_y;
var diff_m = tdy_m - dob_m;
var diff_d = tdy_d - dob_d;

if((diff_m < 0) || (diff_m == 0 && diff_d < 0)) {
diff_y -= 1;
}

return diff_y;
}


You may want to read up a bit more on onsubmit and form validation if you choose the Javascript method.

Hope that helps,
Sadiq.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum