Lee wrote:
Susan Cranford said:
I am more comfortable with VBScript but can use either.
The issue isn't the actual calculation of the age, it's the dealing with
passing/setting the values in the arrays of controls.
As always, I appreciate the help here very much!
Here's an example that assumes that the age text field that
corresponds to "txtDOB1" is named "txtAge1". There's no
validation, so the calculation is too simple to use in
production.
Here's a more robust version, noting the usual caveats re user
date/clock settings and JavaScript availability. If really old
browsers are expected, you may want to provide an alternative to the
getFullYear() function based on getYear().
<script type="text/javascript">
function calcAge( bY, bM, bD ) {
var now = new Date();
var xD = new Date( bY, bM-1, bD );
if ( isNaN(xD.getDate()) ) {
return 'Entered text is not valid';
}
var aY = xD.getFullYear();
var aM = xD.getMonth();
var aD = xD.getDate();
if ( aY != bY || aM != (bM-1) || aD != bD || now < xD ) {
return 'Date is not valid';
}
aY = now.getFullYear() - aY;
aM = now.getMonth() - aM;
aD = now.getDate() - aD;
if ( aD < 0) {
aD = now.getDate( now.setDate(aD) );
aM--;
}
if ( aM < 0 ) {
aM += 12;
aY--;
}
return aY + 'y ' + aM + 'm ' + aD + 'd';
}
// Generic function, uses element name
function doDate( x ) {
var xF = x.form;
var idx = x.name.split('_')[1];
var xY = xF.elements[ 'bY_' + idx ].value;
var xM = xF.elements[ 'bM_' + idx ].value;
var xD = xF.elements[ 'bD_' + idx ].value;
var xA = xF.elements[ 'age_' + idx];
if ( xY && xM && xD )
xA.value = calcAge( xY, xM, xD );
}
// Adds onkeyup function to text inputs in form with name starting
// with 'bY_', 'bM_' or 'bD_'
function initForm( form ){
if ( !document.forms) {
// handle browser that doesn't support forms collection
return;
}
form = document.forms[form];
var els = form.elements;
var el, i = els.length;
while ( i-- ) {
el = els[i];
if ( el.name
&& 'text' == el.type
&& /^b[YMD]_/.test(el.name) ){
el.onkeyup = function() { doDate(this); }
}
}
}
// Just in case user pastes values rather than typing
function calcAll( f ){
f = f.form;
var els = f.elements;
var el, i = els.length;
while ( i-- ) {
el = els[i];
if ( el.name
&& 'text' == el.type
&& el.name.match('bY_') ) {
doDate(el);
}
}
}
window.onload = function() { initForm('ageForm'); }
</script>
<form name="ageForm" action="">
<table border="0">
<tr>
<td colspan="4" style="text-align: center;">Enter
a birth date (yyy mm dd)</td>
</tr><tr>
<td style="text-align: center;">Year</td>
<td style="text-align: center;">Month</td>
<td style="text-align: center;">Day</td>
<td style="text-align: center;">Age (readonly)</td>
</tr>
</tr><tr>
<td><input type="text" name="bY_0" size="4" maxlength=4>/</td>
<td><input type="text" name="bM_0" size="2" maxlength=2>/</td>
<td><input type="text" name="bD_0" size="2" maxlength=2></td>
<td><input type="text" name="age_0" size="20" readonly></td>
</tr>
</tr><tr>
<td><input type="text" name="bY_1" size="4" maxlength=4>/</td>
<td><input type="text" name="bM_1" size="2" maxlength=2>/</td>
<td><input type="text" name="bD_1" size="2" maxlength=2></td>
<td><input type="text" name="age_1" size="20" readonly></td>
</tr>
</tr><tr>
<td><input type="text" name="bY_2" size="4" maxlength=4>/</td>
<td><input type="text" name="bM_2" size="2" maxlength=2>/</td>
<td><input type="text" name="bD_2" size="2" maxlength=2></td>
<td><input type="text" name="age_2" size="20" readonly></td>
</tr>
<tr>
<td colspan="4">
<input type="reset" onclick="if (this.blur) this.blur();">
<input type="button" value="Re-calculate all ages..." onclick="
calcAll(this);
if ( this.blur ) this.blur();
">
</td>
</tr>
</table>
</form>
--
Rob