HighDots Forums  

Re: Pass input Array value to function to calc different input value

Javascript JavaScript language (comp.lang.javascript)


Discuss Re: Pass input Array value to function to calc different input value in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Susan Cranford
 
Posts: n/a

Default Re: Pass input Array value to function to calc different input value - 07-04-2005 , 11:16 PM






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!




*** Sent via Developersdex http://www.developersdex.com ***

Reply With Quote
  #2  
Old   
RobG
 
Posts: n/a

Default Re: Pass input Array value to function to calc different input value - 07-05-2005 , 12:08 AM






Lee wrote:
Quote:
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


Reply With Quote
  #3  
Old   
Susan Cranford
 
Posts: n/a

Default Re: Pass input Array value to function to calc different input value - 07-05-2005 , 01:44 AM




Great! But since the 1, 2...77 is a variable, how would that look ?



*** Sent via Developersdex http://www.developersdex.com ***

Reply With Quote
  #4  
Old   
RobG
 
Posts: n/a

Default Re: Pass input Array value to function to calc different input value - 07-05-2005 , 02:03 AM



Susan Cranford wrote:
Quote:
Great! But since the 1, 2...77 is a variable, how would that look ?
This bit of the script splits out the letter part of the name from the
index part:

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];

In the above:

if x.name is 'bY_77'
then x.name.split('_')[0] is 'bY'
and x.name.split('_')[1] is '77'

The underscore is just a handy delimiter, the key after the delimiter
can be anything that's valid in an element name - numbers are convenient
where an iterating process is likely to be used.

<URL:http://www.w3.org/TR/html4/interact/forms.html#adef-name-INPUT>

The script relates to the elements named thusly:

<td><input type="text" name="bY_77" ...
<td><input type="text" name="bM_77" ...
<td><input type="text" name="bD_77" ...
<td><input type="text" name="age_77" ...

You need to work out how to create unique names for all the form
elements while keeping the 'index' as essentially a kind of foreign key
so that the script can work out which ones are related.

The names can be generated at the server using whatever you use to
generate pages (ASP? PHP?...) or hard-coded if you like.

--
Rob


Reply With Quote
  #5  
Old   
Dr John Stockton
 
Posts: n/a

Default Re: Pass input Array value to function to calc different input value - 07-06-2005 , 12:21 PM



JRS: In article <BInye.2377$Zn.111092 (AT) news (DOT) optus.net.au>, dated Tue, 5
Jul 2005 04:08:01, seen in news:comp.lang.javascript, RobG
<rgqld (AT) iinet (DOT) net.auau> posted :


Quote:
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';
}
I believe that test to be superfluous, since anything failing it will
fail the next test soon enough, if that is rewritten to use == rather
than != ; NaN != anything.

Come to think of it, it should only fail for out-of-range dates; ECMA
allows ~ -271821-04-20 to +275760-09-13 GMT. However, I hear that
Safari 1.2.3 on Mac OS 10.3. shows a range of +- 2^31 seconds from zero
- 1901-12-13 to 2038-01-19 GMT.




Quote:
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';
}
I believe testing Y is superfluous, at least if there are no dates
before AD 100. And date 0000-02-29 can be a problem!



AFAIR, the OP has not defined "age". If only age in years is needed,
consider (Y M D is today; A is input) :-

function PDCount(Y, M, D) { return (Y*20 + +M)*50 + +D }

Age = ( PDCount(Y, M, D) - PDCount(A[2], A[1], A[0]) ) / 1000
Age = Math.floor(Age)

Note the question of when a person born on Feb 29 becomes 18; all should
be well if that's taken as Mar 1, but may well not be if taken as Feb
28. Check applicable legislation.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.


Reply With Quote
  #6  
Old   
Susan Cranford
 
Posts: n/a

Default Re: Pass input Array value to function to calc different input value - 07-06-2005 , 08:15 PM




Thanks so much to everyone who tried to help me with this. I finally
figured out how to get it work as I needed with vbscript. It was
simple, I was just driving all the way around the mountain when I should
have taken the short cut! I got caught up with the index of the input
element when I just had to pass the 2 objects to my sub and wala...it
worked like a charm.

Again thanks to all of you wonderfully helpful folks!



*** Sent via Developersdex http://www.developersdex.com ***

Reply With Quote
Reply




Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.