Pass input Array value to function to calc different input value

Discussion in 'Javascript' started by Susan Cranford, Jul 4, 2005.

  1. 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 ***
    Susan Cranford, Jul 4, 2005
    #1
    1. Advertising

  2. Susan Cranford

    RobG Guest

    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
    RobG, Jul 5, 2005
    #2
    1. Advertising

  3. Susan Cranford

    RobG Guest

    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
    RobG, Jul 5, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Iceman
    Replies:
    1
    Views:
    430
    Steve Pugh
    Jan 7, 2004
  2. Tricky
    Replies:
    0
    Views:
    572
    Tricky
    Mar 19, 2009
  3. Tricky
    Replies:
    0
    Views:
    994
    Tricky
    Mar 19, 2009
  4. Reuven
    Replies:
    0
    Views:
    587
    Reuven
    Mar 19, 2009
  5. Kaye Ng

    ruby calc.rb or just calc.rb

    Kaye Ng, Jul 26, 2010, in forum: Ruby
    Replies:
    7
    Views:
    402
    Kaye Ng
    Jul 28, 2010
Loading...

Share This Page