Probably helps to filter your input with drop-downs.
Note: the document.write()s are for abbreviating this demo (probably want to hardcode the options in your own form).
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>untitled</title>
<script type="text/javascript">
var numDays = {
'1': 31, '2': 28, '3': 31, '4': 30, '5': 31, '6': 30,
'7': 31, '8': 31, '9': 30, '10': 31, '11': 30, '12': 31
};
function setDays(oMonthSel, oDaysSel, oYearSel)
{
var nDays, oDaysSelLgth, opt, i = 1;
nDays = numDays[oMonthSel[oMonthSel.selectedIndex].value];
if (nDays == 28 && oYearSel[oYearSel.selectedIndex].value % 4 == 0)
++nDays;
oDaysSelLgth = oDaysSel.length;
if (nDays != oDaysSelLgth)
{
if (nDays < oDaysSelLgth)
oDaysSel.length = nDays;
else for (i; i < nDays - oDaysSelLgth + 1; i++)
{
opt = new Option(oDaysSelLgth + i, oDaysSelLgth + i);
oDaysSel.options[oDaysSel.length] = opt;
}
}
}
function chkDOB(oMonthSel, oDaysSel, oYearSel)
{
var DOB = [
oMonthSel.options[oMonthSel.selectedIndex].text ,
oDaysSel.options[oDaysSel.selectedIndex].text,
oYearSel.options[oYearSel.selectedIndex].text
];
DOB = new Date(DOB.join(', ')).getTime(); //milliseconds in DOB
var now = new Date().getTime(); //milliseconds in today's date
var milliday = (24 * 60 * 60 * 1000); //milliseconds in one day
if ((now - DOB) < ((13 * 365 * milliday) + (4 * milliday))) //compensate for leap year
{
self.location = 'http://www.drtill.de/wkwh/die1/images/funpictures/underage.gif'; //redirect
return false;
}
else return true;
}
</script>
</head>
<body>
<form action="javascript:alert('old enough')" method="post" onsubmit="return chkDOB(month,day,year)">
<strong>My date of birth was </strong>
<select name="month" id="month" onchange="setDays(this,day,year)">
<option value="1" selected="selected">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select name="day" id="day">
<option value="1" selected="selected">1</option>
<script type="text/javascript">
var d = 1;
while (++d < 32)
document.write('<option value="' + d + '">' + d + '</option>');
</script>
</select>
<select name="year" id="year" onchange="setDays(month,day,this)">
<script type="text/javascript">
var y = 1939;
while (++y < 2003)
document.write('<option value="' + y + '">' + y + '</option>');
</script>
<option value="2003" selected="selected">2003</option>
</select>
<input type="submit" value="next" />
</form>
</body>
</html>
Bookmarks