Code:
<html>
<head>
<script type="text/javascript">
var isToday = "";
function setDateLimit(isForm,isList,nDays){
var prevLimit = isForm[isList].length;
var currLimit = nDays;
if (currLimit < prevLimit)
{isForm[isList].length = currLimit}
else {
nOptions = currLimit-prevLimit
for (i=1; i<nOptions+1; i++)
{
var isText = (prevLimit+i).toString();
insertDates(isForm[isList],isText,isText);
}
}
isForm[isList].selectedIndex = 0;
checkLeap(isForm,isList);
}
function insertDates(isList,isValue,isText){
var nOption = document.createElement('option');
var isData = document.createTextNode(isText);
nOption.setAttribute('value',isValue);
nOption.appendChild(isData);
isList.appendChild(nOption);
}
function checkLeap(isForm,isList){
var isYear = isForm.startYears.value;
if (isYear % 4 == 0 && isForm['startMonths'].value == '28')
{
nOption = document.createElement('option');
isData = document.createTextNode('29');
nOption.setAttribute('value','29');
nOption.appendChild(isData);
isForm[isList].appendChild(nOption);
}
}
function resetBoth(isForm,isMonths,isDates){
isForm[isMonths].selectedIndex = 0;
isForm[isDates].options.length = 0;
for (i=1; i<32; i++)
{
insertDates(isForm[isDates],i,i)
}
}
function clipDates(isForm,isMonth,isList){
var nDates = isForm[isMonth].value;
if (nDates == '30')
{isForm[isList].options.length = 30}
if (nDates == '28')
{isForm[isList].options.length = 28}
if (isForm.startYears.value % 4 == 0 && isForm[isMonth].value == '28')
{
var nOption = document.createElement('option');
var isData = document.createTextNode('29');
nOption.setAttribute('value','29');
nOption.appendChild(isData);
isForm[isList].appendChild(nOption);
}
}
function setUp(){
isToday = new Date();
var nStart = isToday.getMonth()+1+"/"+isToday.getDate()+"/"+isToday.getFullYear();
var isStart = nStart.split("/");
document.forms[0].startMonths.selectedIndex = isStart[0]-1;
document.forms[0].startDates.selectedIndex = isStart[1]-1;
var yearOffset = (1970-isStart[2])*-1;
document.forms[0].startYears.selectedIndex = yearOffset;
clipDates(document.forms[0],'startMonths','startDates');
document.forms[0].birthDate.value = nStart;
}
function insertSlashDate(isForm){
var startStr = isForm.startMonths.selectedIndex+1+"/";
startStr += isForm.startDates.value+"/";
startStr += isForm.startYears.value;
startStr = startStr.replace(/^(\d{1}\/)/,"0$1").replace(/(\d{2}\/)(\d{1}\/)/,"$10$2")
isForm.birthDate.value = startStr;
}
function validate(nForm){
var birthDay = new Date(nForm.birthDate.value);
var age = isToday.getFullYear()-birthDay.getFullYear();
var currYrbDay = new Date(isToday.getFullYear(),birthDay.getMonth(),birthDay.getDate())
if (currYrbDay > isToday && age > 0){age--}
if (age < 13){alert('Access denied');return false}
alert(nForm.birthDate.value);
}
onload=setUp;
</script>
</head>
<body>
<br>
<form onsubmit="return validate(this)">
<Table align='center' cellspacing='0' cellpadding='5' style='font-size:12pt;border:solid black 1px;background-color:lightyellow'>
<Thead><TH colspan='4' bgcolor='moccasin'> Submit your birthday </TH></Thead>
<td>
<?php
$months = array (0 => 'January','February','March','April','May','June','July','August','September','October','November','December');
$dayLimit = array('31','28','31','30','31','30','31','31','30','31','30','31');
?>
<select name='startMonths' onchange="setDateLimit(this.form,'startDates',this.value);insertSlashDate(this.form)">
<?php
foreach ($months as $key => $value) {
echo "<option value='$dayLimit[$key]'>$value</option>\n";
}
?>
</select>
</td>
<td>
<? $days = range (1, 31) ?>
<select name='startDates' onchange="insertSlashDate(this.form)">
<?php
foreach ($days as $value) {
echo "<option value=\"$value\"> $value</option>\n";
}
?>
</select>
</td>
<td>
<?php $years = range (1970, 2010) ?>
<select name='startYears' onchange="resetBoth(this.form,'startMonths','startDates');insertSlashDate(this.form)">
<?php
foreach ($years as $value) {
echo "<option value=\"$value\"> $value</option>\n";
}
?>
</select>
</td>
</tr>
<tr>
<td colspan='3' align='middle'>
<input type='submit' value="Submit">
</td>
</tr>
</table>
<input type='hidden' name='birthDate'>
</form>
</body>
</html>
Bookmarks