SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form field validation

    Hello,

    I wrote a php page which contains some input fields of type text which are generated by using a for loop in php. The names for the input fields are generated by appending the value incremented in the for loop in php. eg. st0,st1,st2-----. Now i need to check using javascript that some value is entered in the input fields. How can i do that using a for loop in javascript.

    The php code i used to generate the text fields are:

    <form name=f method=post action=test.php>
    <?php
    $count=5;
    for($i =0;$i<count;$i++)
    {
    echo "<input type=text name="st".$i>";
    }
    ?>
    </form>


    The html o/p for the above code will be:

    <form name=f method=post action=test.php>
    <input type=text name=st0>
    <input type=text name=st1>
    <input type=text name=st2>
    <input type=text name=st3>
    <input type=text name=st4>
    </form>

    How can i check that whether a value is entered into the fields or not in javascript

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Portsmouth UK
    Posts
    1,499
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd"
    >

    <
    html>

    <
    head>
      <
    title></title>
    <
    script language="JavaScript" type="text/javascript">
    <!--
    function 
    Check(){
     for (
    i=0;i<5;i++){
      if (
    document.f['st'+i].value.length<1){
       
    alert('st'+i+' is empty');
      }
     }

    }
    //-->
    </script>
    </head>

    <body  >
    <form name=f method=post action=test.php>
    <input type=text name=st0>
    <input type=text name=st1>
    <input type=text name=st2>
    <input type=text name=st3>
    <input type=text name=st4>

    </form>
    <input type="button" name="" value="Check" onclick="javascript:onload=Check();">


    </body>

    </html> 

  3. #3
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    Thanks for the post, that helped me solve it.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •