How to validate multiple dynamically named form fields?

Discussion in 'Javascript' started by phpCodeHead, Apr 4, 2007.

  1. phpCodeHead

    phpCodeHead Guest

    I am needing to determine how to go about validating that a field in
    my form contains only a positive integer. I know that this is fairly
    simple if the form contains only one element to be validated; but, a
    much bigger challenge ( to me anyway, that's why I'm coming to the
    pros! ) when I don't know exactly how many fields may appear on the
    form as it is dynamically generated based upon the number of line
    items to be received on a purchase order. (i.e. if there are 10 items
    to receive in; I will have 10 text fields to enter "quantity received"
    into. Obviously, I want to be sure that only a positive integer is
    accepted into these fields.

    I apologize for pasting in SO MUCH of my code, but it gives you the
    insight as to how my form elements are getting named...

    The field I'm needing to validate is referenced in the code below as:
    <td><input type="text" size="7" maxlength="6" name="<?php echo
    'amtReceived_'.$poLineItems[VItemID]; ?>" value="" /></td>

    Here is the code that generates the dynamic number of table rows/form
    elements:
    <?php
    while ($poLineItems = mysql_fetch_assoc($result)) {
    ?>
    <tr class="lineitem">
    <td><?= $poLineItems[VItemID] ?></td>
    <td align="left"><?= $poLineItems[CDescription] ?></td>
    <td><?= $poLineItems[OnOrder] ?></td>
    <td><?= $poLineItems[OnHand] ?></td>
    <td><input type="text" size="7" maxlength="6" name="<?php echo
    'amtReceived_'.$poLineItems[VItemID]; ?>" value="" /></td>
    <td>
    <select name="<?php echo 'location_'.$poLineItems[VItemID]; ?>">
    <?php

    $stmt2 = "SELECT DISTINCT City, Zip FROM shipto WHERE CompanyID
    = 169";
    $result2 = mysql_query($stmt2);

    while($rowDD = mysql_fetch_row($result2)) {
    if($rowDD[0] == 'Rosemont') {
    echo '<option value="'.$rowDD[0].'" selected>'.
    $rowDD[0].', '.$rowDD[1].'</option>';
    } else {
    echo '<option value="'.$rowDD[0].'">'.$rowDD[0].', '.
    $rowDD[1].'</option>';
    }
    }
    ?>
    </select>
    </td>
    </tr>
    <?php
    }
    ?>
    </table>
    <p><input type="submit" value="Receive Items" method="POST" /></p>
    <?php
    } else {
    ?>

    <p><div class="nogo">No items found for PO number <?=
    $receive_ponum ?> </div><br />
    <input type="button" value="<< Go Back <<"
    onclick="history.back()" />
    </p>
    <?php
    }
    ?>

    Thanks for any help whatsoever......

    Gene
     
    phpCodeHead, Apr 4, 2007
    #1
    1. Advertisements

  2. phpCodeHead

    -Lost Guest

    <snip PHP code mostly>

    None of your PHP code has anything to do with JavaScript.

    The only thing that matters is you address your form's elements collection. For example:

    <form name="form1">
    <input type="text" name="text1" />
    <input type="text" name="text2" />
    <input type="submit" value="Submit" />
    </form>

    document.forms['form1'].elements is a collection of all elements within that form. You
    could then iterate those elements, excluding submit, and check your inputs values.

    You might use parseInt() and check whether or not it is less than 1 (or 0, depending upon
    what you consider negative).

    As a last aside, I would not mix traditional PHP start and end tags with ASP or ECHO-style
    tags.

    -Lost
     
    -Lost, Apr 5, 2007
    #2
    1. Advertisements

  3. phpCodeHead

    RobG Guest

    parseInt is ineffective when checking that only digits have been input
    since it will ignore trailing non-digit characters. Much better to
    use a regular expression, e.g.:

    To check that a string contains zero or more digits, and only digits:

    function checkOnlyDigits( n ){
    return !/\D/.test(n);
    }


    To check that it is a non-negative integer, test that it contains one
    or more digits, and only digits:

    function validateIntegerNN( n ){
    return /^\d+$/.test(n);
    }

    There are many more examples on JRS's site:

    <URL: http://www.merlyn.demon.co.uk/js-valid.htm#VNP >


    For the OP, an example implementation (purely a trivial example, a
    more sophisticated validation routine should be employed in
    production) to check for a non-negative integer (i.e. zero or higher)
    is:

    <script type="text/javascript">

    function validateIntegerNN( n ){
    return /^\d+$/.test(n);
    }

    </script>

    <input type="text" onblur="alert( validateIntegerNN(this.value) );">


    The alert will show 'true' only if the input contains at least one
    digit, and only digits, when it loses focus. Otherwise, it will show
    false.

    The exception is that valid integers such as 3e3 (3000) will be
    rejected.
     
    RobG, Apr 5, 2007
    #3
  4. phpCodeHead

    Cah Sableng Guest

    How about:
    /^\d+(e\d+)?$/i
     
    Cah Sableng, Apr 5, 2007
    #4
  5. phpCodeHead

    RobG Guest

    Fill yer boots :)

    The link I provided has lots of examples, I just wanted to point out a
    restriction of the code I posted.
     
    RobG, Apr 5, 2007
    #5
  6. In comp.lang.javascript message <
    !(S%1) looks OK at first sight for testing whether Number(S) has an
    integer value :) unless S amounts to NaN :-( .
     
    Dr J R Stockton, Apr 5, 2007
    #6
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.