View Single Post
Old 11-24-2005, 06:15 AM   PM User | #16
boxxer03
Regular Coder

 
Join Date: Sep 2005
Location: Chicago, IL
Posts: 160
Thanks: 0
Thanked 0 Times in 0 Posts
boxxer03 is an unknown quantity at this point
I took the original code and changed it a little and this works perfect. The only problem is like PhotoJoe said, is that say you were born in 1984 then any month or day you use for this year will return 21 even if you haven't had your b-day yet. You need to add a couple lines that pretty much checks to see if the month/day they entered is before or on this day and if it isn't then subtract 1.

Code:
<script type="text/javascript">
<!--
function calcAge() {

var dob_month;
var dob_day;
var dob_year;
var dob;

dob = document.form1.elements.dob.value;

var birthDay = dob.split("-");

dob_month = birthDay[0];
dob_day = birthDay[1];
dob_year = birthDay[2];

var time=new Date();
var lmonth=time.getMonth() + 1;
var date=time.getDate();
var year=time.getFullYear();

var returnAge = year - dob_year;

document.form1.r_age.value=returnAge

alert("Your age is: " + returnAge);

}
//-->
</script>


<body>

<form name="form1">

<p>Enter date of birth in format MM-DD-YYYY
<input id="dob" type="text" name="dob" value="">
</p>

<p id="itext">
Click the button below to calculate your age
</p>

<p>
<input type="button" value="Calculate" onclick="calcAge()">
</p>

<p>
Your age is: <input type="text" name="r_age" size="10">
</p>
</form>
__________________
I'm only telling you how I would do it, not how its supposed to be done. ;)
boxxer03 is offline   Reply With Quote