473,419 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

Pass input Array value to function to calc different input value

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 ***
Jul 23 '05 #1
12 6958
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
Jul 23 '05 #2

Easily doable, but you need to be much more specific on what you have and
what you need. Calculations are simple depending on what your source
might be, but the calculation are simple, just be concise and explainful.

Danny

On Mon, 04 Jul 2005 15:50:11 -0700, Susan Cranford <ne******@comcast.net>
wrote:


--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Jul 23 '05 #3
Sure. I will have several text input boxes created on the fly using
loops to create these varying number of dob's. When a dob on one of
them is entered (txtDOB1, txtDOB2...) I want to calc the age (I was
tying to use vbscript and datediff to try and do this with an onchange
event to call the function) and then put the age in a readonly text box
right beside each of the txtDOBn input boxes.

Is that enough info ?

*** Sent via Developersdex http://www.developersdex.com ***
Jul 23 '05 #4
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
Jul 23 '05 #5
Lee
Susan Cranford said:

Sure. I will have several text input boxes created on the fly using
loops to create these varying number of dob's. When a dob on one of
them is entered (txtDOB1, txtDOB2...) I want to calc the age (I was
tying to use vbscript and datediff to try and do this with an onchange
event to call the function) and then put the age in a readonly text box
right beside each of the txtDOBn input boxes.

Is that enough info ?


Are you asking how to do this in VBScript, or Javascript?

Jul 23 '05 #6
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 ***
Jul 23 '05 #7
Lee
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.

<html>
<head>
<title>demo</title>
<script type="text/javascript">
function setAge(dobFieldRef) {
var ageFieldName=dobFieldRef.name.replace(/DOB/,"Age");
var ageFieldRef=dobFieldRef.form.elements[ageFieldName];
var dobDate=new Date(dobFieldRef.value);
var todaysDate=new Date();
var age=todaysDate.getFullYear()-dobDate.getFullYear();
var monthDiff=dobDate.getMonth()-todaysDate.getMonth();
var dateDiff=dobDate.getDate()-todaysDate.getDate();
if (monthDiff>0 || monthDiff==0 && dateDiff>0) age--;
ageFieldRef.value=age;
}
</script>
</head>
<body>
<form>
<input name="txtDOB1" onchange="setAge(this)"><input name="txtAge1"><br>
<input name="txtDOB2" onchange="setAge(this)"><input name="txtAge2"><br>
<input name="txtDOB77" onchange="setAge(this)"><input name="txtAge77"><br>
</form>
</body>
</html>

Jul 23 '05 #8
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
Jul 23 '05 #9

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

*** Sent via Developersdex http://www.developersdex.com ***
Jul 23 '05 #10
Susan Cranford wrote:
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
Jul 23 '05 #11
JRS: In article <BI******************@news.optus.net.au>, dated Tue, 5
Jul 2005 04:08:01, seen in news:comp.lang.javascript, RobG
<rg***@iinet.net.auau> posted :

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.

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.
Jul 23 '05 #12

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 ***
Jul 23 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Fred | last post by:
Hi, i know how to pass a value from Javascript to ASP with a hidden field into a form and submitting it, or with cookies, but here i have to pass a lot of data in an array. There is a list of...
4
by: Sunny | last post by:
Hi, I am at present working on a jsp page which has a single text field. Upon entering a value in the field, i am trigerring an onChangeEvent() which calls a method of javascript. I am also...
4
by: Dan | last post by:
Can anyone offer suggestions on how to do this or if it is possible? I have a form that uses a drop down box and 2 text fields. What I am trying to do is have the value of each text box set by...
17
by: Kermit Piper | last post by:
Hello, I have been searching, Googling, searching. Cannot find a javascript to calc 10, 11 or 12 digit UPC codes. I just need an algorithm that calcs to verify the correct check digit. I have...
3
by: carvalho.miguel | last post by:
hello, imagine you have a static class method that receives a function pointer, an int with the number of arguments and a variable number of arguments. in that static method you want to call...
19
Atli
by: Atli | last post by:
Introduction At some point, all web developers will need to collect data from their users. In a dynamic web page, everything revolves around the users input, so knowing how to ask for and collect...
3
by: docmur | last post by:
Okay heres my problem I have a problem called Calc which the user enters 2 numbers and a sign to be done with what is entered EX: ./calc 200 + 200 or ./calc 200.20 + 200 Okay thats easy...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.