Question on For Loops Using ID Number

Discussion in 'Javascript' started by Rick, Oct 24, 2005.

  1. Rick

    Rick Guest

    Hello,

    I posted a question regarding data validation earlier and it worked
    great! I have a new question. Is there anyway to cut through all the
    if statements and just have a for loop that uses the id numbers
    associated with each input. Here's the code I have so far:

    <html>
    <head>
    <script type="text/javascript">

    function main(form)
    {
    if (form.description.value =='')
    {
    alert("you have not entered in a description");
    return false;
    }
    if (form.awarddate.value =='')
    {
    alert("you have not entered in an award date2");
    return false;
    }
    if(form.awarddate2.value =='')
    {
    alert("you have not entered in an award date2");
    return false;

    return true;
    }

    </script>
    </head>
    <body>
    <form action="submit.html" method="post" name="input" onsubmit="return
    main(this)">
    <table>
    <tr>
    <th>Rick's Test Page<br><br></th>
    </tr>
    <tr>
    <td>Description:<td><input type=text name="description" id=1>
    </tr><tr>
    <td>Award Date:<td><input type=text name="awarddate" id=2>
    </tr><tr>
    <td>Award Date 2:<td><input type=text name="awarddate2" id=3>
    </tr>
    </table>
    <input type="submit" value="Submit" class="FormText">
    </form>
    </body>
    </html>

    So In this case I'd want to make a for loop that loops from id=1 to
    id=3. Is there anyway to do that? Thanks for the help!

    Rick
    Rick, Oct 24, 2005
    #1
    1. Advertising

  2. Rick

    McKirahan Guest

    "Rick" <> wrote in message
    news:...
    > Hello,
    >
    > I posted a question regarding data validation earlier and it worked
    > great! I have a new question. Is there anyway to cut through all the
    > if statements and just have a for loop that uses the id numbers
    > associated with each input. Here's the code I have so far:


    [snip]

    You're code is missing a "}" before "return true;".

    You're missing closing </td> tags.

    Don't use "main()" as it's a rserved word (per Evertjan).

    Though you can use "document.getElementById()" it isn;t necessary.

    The following loops through all form elements and checks all "text"
    elements to see if they're blank.

    Also, the fields name (name=" is displayed in the alert so it is suggested
    that you assign a anme similar to the label they'll see on the page.

    <html>
    <head>
    <script type="text/javascript">
    function validate(form) {
    var fail = "";
    for (i=0; i<form.elements.length; i++) {
    if (form.elements.type == "text") {
    if (form.elements.value == "") {
    fail += "\n\t" + form.elements.name + "";
    }
    }
    }
    if (fail != "") {
    alert("The following fields are blank:\n" + fail);
    return false;
    }
    return true;
    }
    </script>
    </head>
    <body>
    <form action="submit.html" method="post"
    name="input" onsubmit="return validate(this)">
    <table>
    <tr>
    <th colspan="2">Rick's Test Page<br><br></th>
    </tr>
    <tr>
    <td>Description:</td>
    <td><input type=text name="Description" id=1></td>
    </tr>
    <tr>
    <td>Award Date:</td>
    <td><input type=text name="Award_Date" id=2></td>
    </tr>
    <tr>
    <td>Award Date 2:</td>
    <td> <input type=text name="Award_Date_2" id=3></td>
    </tr>
    </table>
    <input type="submit" value="Submit" class="FormText">
    </form>
    </body>
    </html>
    McKirahan, Oct 24, 2005
    #2
    1. Advertising

  3. Rick

    Rick Guest

    Amazing!

    Thank you so much!
    Rick, Oct 24, 2005
    #3
  4. Rick

    RobG Guest

    McKirahan wrote:
    > "Rick" <> wrote in message
    > news:...
    >
    >>Hello,
    >>
    >>I posted a question regarding data validation earlier and it worked
    >>great! I have a new question. Is there anyway to cut through all the
    >>if statements and just have a for loop that uses the id numbers
    >>associated with each input. Here's the code I have so far:

    >
    >
    > [snip]
    >
    > You're code is missing a "}" before "return true;".
    >
    > You're missing closing </td> tags.
    >
    > Don't use "main()" as it's a rserved word (per Evertjan).
    >
    > Though you can use "document.getElementById()" it isn;t necessary.
    >
    > The following loops through all form elements and checks all "text"
    > elements to see if they're blank.
    >
    > Also, the fields name (name=" is displayed in the alert so it is suggested
    > that you assign a anme similar to the label they'll see on the page.


    [...]

    > <td><input type=text name="Description" id=1></td>


    And ID's can't start with a number, an id:

    "must begin with a letter ([A-Za-z]) and may be followed by any number
    of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons
    (":"), and periods (".")."

    <URL:http://www.w3.org/TR/html4/types.html#type-name>

    [...]


    --
    Rob
    RobG, Oct 24, 2005
    #4
  5. Rick

    McKirahan Guest

    "RobG" <> wrote in message
    news:435d4e2a$0$8620$...

    [snip]

    > > <td><input type=text name="Description" id=1></td>

    >
    > And ID's can't start with a number, an id:
    >
    > "must begin with a letter ([A-Za-z]) and may be followed by any number
    > of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons
    > (":"), and periods (".")."
    >
    > <URL:http://www.w3.org/TR/html4/types.html#type-name>
    >
    > [...]
    >


    Also, "id=" is usually set to the same value as "name=".
    McKirahan, Oct 24, 2005
    #5
  6. Rick

    Evertjan. Guest

    McKirahan wrote on 24 okt 2005 in comp.lang.javascript:

    > Also, "id=" is usually set to the same value as "name=".
    >


    Not at all.

    In a form, the name goes into the querystring/poststring,
    and could be out of control of the programmer on a external site,
    the id however could be seralized.

    <input name='theName' id='f1'>
    <input name='theStreet' id='f2'>
    <input name='theTown' id='f3'>
    ......

    for large forms, the clientside code can be much simpler that way:

    for (i=1;i<60;i++){
    with (document.getElementById('f'+i)){
    if (value != defaultValue)
    style.backgroundColor='cyan'
    else
    style.backgroundColor=''
    }
    }

    yes I know, IE only can do:

    input { background-color:expression(value!=defaultValue?'cyan':'');}

    --
    Evertjan.
    The Netherlands.
    (Replace all crosses with dots in my emailaddress)
    Evertjan., Oct 25, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Allan Bruce

    dynamic number of nested loops

    Allan Bruce, Jul 1, 2004, in forum: Java
    Replies:
    5
    Views:
    9,757
    Tor Iver Wilhelmsen
    Jul 3, 2004
  2. Klaas Vantournhout

    unkown number of nested for loops

    Klaas Vantournhout, Sep 21, 2006, in forum: C Programming
    Replies:
    3
    Views:
    322
    Lew Pitcher
    Sep 21, 2006
  3. vary number of loops

    , Apr 16, 2008, in forum: Python
    Replies:
    6
    Views:
    288
  4. Me
    Replies:
    2
    Views:
    237
  5. Replies:
    2
    Views:
    141
    Laurent Bugnion
    Aug 22, 2006
Loading...

Share This Page