Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Aug 2002
    Posts
    151
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow Loop through divs and check that input fields have been filled in?

    I have the following structure

    <div id="rows">

    <div id="row-0" class="row">
    <input name="firstname[]" class="firstname" type="text" />
    <input name="lastname[]" type="text" class="lastname" />
    <input name="email[]" type="text" class="email" />
    </div>

    <div id="row-1" class="row">
    <input name="firstname[]" class="firstname" type="text" />
    <input name="lastname[]" type="text" class="lastname" />
    <input name="email[]" type="text" class="email" />
    </div>


    and so on... the number of these div rows is dynamic

    </div>



    What I'd like to do is loop through each div row ("row-0", "row-1"..) and check that both the firstname and lastname fields have been filled in. If not, then I would like to display a generic message and place the cursor in the blank field.

    Does anyone have any suggestions on how to use this?

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Code:
    function validate(f){
      var fnames = f.elements["firstname[]"];
      var lnames = f.elements["lastname[]"];
      var len = fnames.length;
      for (var i=0; i<len; i++){
        if (fnames[i].value==""){
           alert("First name is required");
           fnames[i].focus();
           return false;
        }
        if (lnames[i].value==""){
           alert("Last name is required");
           lnames[i].focus();
           return false;
        }
      }
      return true;
    }
    ...
    <form ... onsubmit="return validate(this)">
    ...
    <input type="submit" name="btnSubmit" value="Submit" />
    </form>
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    Regular Coder
    Join Date
    Aug 2002
    Posts
    151
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I see. Awesome.

    Thanks


  •  

    Posting Permissions

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