Pass input Array value to function to calc different input value

S

Susan Cranford

Please forgive, I have looked at so much info I can't figure out how to
put it together even though I know it must be fairly simple.

I have an array of input text boxes (txtDOBn) where n is created at
load. On the onchange event I want to calc the age and show in adjacent
input text boxes that are readonly and also arrays (an age calced for
each DOB entered). I was going to use the datediff function in vbscript
to do the calc.

Can someone please help me pull the pieces together or point me to a
similiar example?

I didn't post HTML because of message about spam.
 
R

RobG

Susan said:
Please forgive, I have looked at so much info I can't figure out how to
put it together even though I know it must be fairly simple.

I have an array of input text boxes (txtDOBn) where n is created at
load. On the onchange event I want to calc the age and show in adjacent
input text boxes that are readonly and also arrays (an age calced for
each DOB entered). I was going to use the datediff function in vbscript
to do the calc.

Can someone please help me pull the pieces together or point me to a
similiar example?

Have a read of Dr J's pages:

<URL:http://www.merlyn.demon.co.uk/js-dates.htm>
 
R

RobG

Susan said:
Please forgive, I have looked at so much info I can't figure out how to
put it together even though I know it must be fairly simple.

I have an array of input text boxes (txtDOBn) where n is created at
load. On the onchange event I want to calc the age and show in adjacent
input text boxes that are readonly and also arrays (an age calced for
each DOB entered). I was going to use the datediff function in vbscript
to do the calc.

Can someone please help me pull the pieces together or point me to a
similiar example?

I didn't post HTML because of message about spam.

Here is a function that validates user input an calculates an age in
years, months and days. It uses the local PC date, which may not be
correct and the clock may not be accurately set.

It makes use of date objects to do calculations and so maybe slow, but
for a form where you are doing them one at a time with user input in
between the time will go un-noticed. Search the archives here for
plenty of other solutions.

Date validation creates a date object from the input text. If it's a
valid date, is the same as the input date (e.g. 31 June is a 'valid'
date, but converts to 01 July so not valid in this case) and isn't in
the future, the date is accepted as valid. Using a date object to
check the date means we don't have to manually deal with leap years,
daylight saving, etc.

The age is calculated as the difference in years (always 0 or greater),
difference in date (adjusting the months & date if difference is
negative) and difference in months (adjusting the year & month if month
difference is negative).

<script type="text/javascript">

// Calculates age in years, months & days
// Assumes system clock is accurate...
function calcAge( f ) {

// Get entered values
f = f.form;
var bY = f.bY.value;
var bM = f.bM.value;
var bD = f.bD.value;
var now = new Date();

// Generate date from input
var xD = new Date( bY, bM-1, bD );

// Check generated date is OK
if ( isNaN(xD.getDate()) ) {
return 'Entered text is not valid';
}

// Get date components
var aY = xD.getFullYear();
var aM = xD.getMonth();
var aD = xD.getDate();

// Validate date entered
if ( aY != bY || aM != (bM-1) || aD != bD || now < xD ) {
return 'Date is not valid';
}

// Calculate age
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';
}
</script>

<form name="ageForm" action="">
/<input name="bM" size="2" maxlength="2"
/<input name="bD" size="2" maxlength="2"><br>
<input type="button" value="Calculate age..." onclick="
this.form.age.value = calcAge(this);
"><input type="reset"><br>
<input type="text" size="20" name="age" readonly>
</div>
</form>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top