Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Pass input Array value to function to calc different input value

Reply
Thread Tools

Pass input Array value to function to calc different input value

 
 
Susan Cranford
Guest
Posts: n/a
 
      07-04-2005
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.


*** Sent via Developersdex http://www.developersdex.com ***
 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      07-04-2005
Susan Cranford wrote:
> 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>


--
Rob
 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      07-05-2005
Susan Cranford wrote:
> 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="">
<div><p>Enter your birth date (YYYY-MM-DD)</p>
<input name="bY" size="4" maxlength="4"
>/<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>


--
Rob
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ruby calc.rb or just calc.rb Kaye Ng Ruby 7 07-28-2010 05:21 AM
Re: Variable Input on procedure - pass by value or pass by reference? Reuven VHDL 0 03-19-2009 07:21 PM
Re: Variable Input on procedure - pass by value or pass by reference? Tricky VHDL 0 03-19-2009 03:59 PM
Variable Input on procedure - pass by value or pass by reference? Tricky VHDL 0 03-19-2009 03:58 PM
Pass a form value that is different from selected value Iceman HTML 1 01-07-2004 03:29 PM



Advertisments