SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form Validation with Multiple Rows

    I've done it before, but can't remember or find how I did it. I know it involved naming the html form inputs as col001row001, though. Doing that dynamically in asp is no problem, but how do I get the row number to be recognized inside javascript's "document.form.column###.value" while going through a for loop?

  2. #2
    SitePoint Zealot
    Join Date
    Aug 2001
    Location
    DC
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmmmmm not exactly sure what you are trying to do. I cannot remember exactly how but you could use the "id" or "name" attribute maybe? It would uniquely identify each element so you could then grab it from the DOM.

    You could use the document.getElementById() method to get any particular element identified using the id attribute of the tag. However only newer browsers will recognize this so check first . .

    if (document.getElementById)
    {
    var nodeObj = document.getElementById('id of element')
    }

    Hope that helps. Why do you have to have a for loop?
    Last edited by Buchanp; Apr 30, 2002 at 23:52.
    signature

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you need this to work with most browsers try:

    for (i=0;i<document.forms[yourformname].elements.length;i++){
    if (document.forms[yourformname].elements[i].value=='') // this is asuming a text input
    // your action here
    else
    // your alternate action here
    }

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, let me see if I can explain better.

    I have a form & table that is created dynamically, meaning I will never know how many rows will be created. Below is an example of the dynamic form created in ASP:

    <form name=form method=post action=update.asp onSubmit="return validate()">
    <table>
    <tr>
    <td><input type=text name=client_id0 value=1></td>
    <td><input type=text name=first_name0 value=Hannibal></td>
    <td><input type=text name=last_name0 value=Lecter></td>
    </tr>
    <td><input type=text name=client_id1 value=2></td>
    <td><input type=text name=first_name1 value=Jodie></td>
    <td><input type=text name=last_name1 value=Foster></td>
    </tr>
    <tr><td><input type=submit value=Save></td></tr>
    </table>
    </form>

    Now, in my JS validate() function I want to be able to 1) know how many rows I am dealing with (haven't found a pretty way to do this, although I do have a way), 2) loop through these rows and check each column. The way I remember doing this was:

    function validate() {
    var counter = handed from the form in a gross manner
    for(i = 0; i < counter; i++){
    if (document.form.client_id + counter + .value=="")) {
    alert("empty")
    }
    }
    }

    Obviously this doesn't work, but it's what I remember. Last night I was playing with this:

    function validate() {
    for(i = 0; i < form.length; i++){
    if (form[i].name == "client_id" + i) {
    alert(form[i].name)
    }
    }
    }

    But I'm assuming form.length is the number of inputs for the entire form (1600+ in the form I'm working on), meaning I'd be out of bounds very quickly (and wasting processing time by looping through unnecessary fields). This is why I was trying to dynamically name the form element. Any ideas?

    (Excuse the code if there's a compile error, I didn't check it, but you get the idea).

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Found my solution. Not sure if it's the prettiest way, but here's the JS:

    function validate() {
    for(i = 0; i < document.form.rowcount.value; i++){
    if (document.getElementById("client_id" + i).value=="") {
    alert("empty");
    document.getElementById("client_id" + i).focus();
    return false;
    }
    }
    }

    I guess I should have payed more attention to the post by Buchanp. Thanks!


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
  •