Dynamic form validation with JavaScript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cfgilles
    New Member
    • Dec 2008
    • 1

    Dynamic form validation with JavaScript

    Hi,

    I have a query that displays each row in a table. Each row is a form and I want to validate with JavaScript each form. How can I dynamically create functions ? Here's my code that does not work..

    Thanks

    Gilles

    [code=html]
    <script type="text/javascript">
    <!--
    function noEntry2(form_n ame) {
    PER_DEB_01 = document.form_n ame.PER_DEB_01. value;
    PER_DEB_02 = document.form_n ame.PER_DEB_02. value;
    PER_DEB_03 = document.form_n ame.PER_DEB_03. value;

    var PER_DEB = new Date(2008, 01, 01);
    var PER_DEB2 = new Date(document.f orm_name.PER_DE B_01.value, document.form_n ame.PER_DEB_02. value, document.form_n ame.PER_DEB_03. value);


    if ( (PER_DEB_01.len gth < 4) || (PER_DEB_02.len gth < 2) || (PER_DEB_03.len gth < 2) ) {
    alert("bla bla bla");
    document.form_n ame.PER_DEB_01. focus();
    return false;
    }
    else if (PER_DEB2 < PER_DEB) {
    alert("bla bla bla");
    return false;
    }

    }

    function interval1(form_ name) {
    var DT_DEB_02 = eval('document. '+form_name+'.P ER_DEB_02.value ');

    if (DT_DEB_02 < 1 || DT_DEB_02 > 12) {
    alert("bla bla bla");
    document.form_n ame.PER_DEB_02. value = ""; // NOT WORKING
    return false;
    }
    else {
    return true;
    }
    }

    function interval2(form_ name) {
    var DT_DEB_02 = eval('document. '+form_name+'.P ER_DEB_03.value ');

    if (DT_DEB_03 < 1 || DT_DEB_03 > 12) {
    alert("bla bla bla");
    document.form_n ame.PER_DEB_03. value = ""; // NOT WORKING
    return false;
    }
    else {
    return true;
    }
    }

    //-->
    </script>




    <table width="98%" align="center" border="0" cellpadding="2" cellspacing="0" >

    <?php
    $ctr = 1;
    while ($recz3 = oci_fetch_array ($z3)) {
    echo "
    <tr>
    <form action='save.ph p' name='form".$ct r."' method='post' onSubmit='retur n noEntry2(form_n ame);'>
    <td>".$recz3["MEDIA"]."</td>
    <td>
    <table border='0' cellspacing='0' cellpadding='0' >
    <tr>
    <td><input name='PER_DEB_0 1' value=".substr( $recz3["PER_DEB"], 0, 4)." type='text' class='idDate' onKeyUp='return autoTab(this, 4, event);' size='4' maxlength='4' style='width: 40px;'></td>
    <td>-</td>
    <td><input name='PER_DEB_0 2' value=".substr( $recz3["PER_DEB"], 5, 2)." type='text' class='idDate' onKeyUp='return autoTab(this, 2, event);' size='2' maxlength='2' style='width: 30px;'></td>
    <td>-</td>
    <td><input name='PER_DEB_0 3' value=".substr( $recz3["PER_DEB"], 8, 2)." type='text' class='idDate' onKeyUp='return autoTab(this, 2, event);' size='2' maxlength='2' style='width: 30px;'></td>
    </tr>
    </table>
    </td>
    <td align='center'> <input name='submit' type='image' value='save' src='images/icone_save_v4.g if'></td>
    </form>
    </tr>
    ";
    $ctr++;
    }
    ?>
    </table>
    [/code]
    Last edited by gits; Dec 10 '08, 07:48 PM. Reason: added code tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    First step is:
    Code:
    document.forms[form_name]..
    which will use the form name passed to the function rather than the literal "form_name" which most likely does not exist.

    Comment

    Working...