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 6 of 6
  1. #1
    New Coder
    Join Date
    Feb 2006
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Field Validation (ARRAY)

    Hi

    I am a PHP programmer. I have written a script that takes values from a form into DB etc. pretty basic. In the HTML form there is a field array,

    <input type="text" name="rafname[]" value="">

    I am writing a script in JavaScript to check that a value is in each of the fields, but the following does not work:

    if (document.rafform.rafname[0].value == "") {
    DO WOTEVA
    }

    Any ideas?

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,518
    Thanks
    3
    Thanked 506 Times in 493 Posts
    ips=document.getElementsByName('rafname[]');

    ips will be a collection(array) of elements with a name of 'rafname[]'
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    New Coder
    Join Date
    Feb 2006
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok great, so i would access as such "ips[0]" "ips[1]" and so forth?

    I actually just tried the following and failed!

    if (ips[0].value == "")
    Last edited by glenelkins; 07-24-2006 at 05:31 PM.

  • #4
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,518
    Thanks
    3
    Thanked 506 Times in 493 Posts
    sorry about that
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    
    <body>
    <input value="000"  />
    <input value="111"  />
    <input value="222"  />
    <input value="333"  />
    
    <script language="JavaScript" type="text/javascript">
    /*<![CDATA[*/
    ips=document.getElementsByTagName('BODY')[0].getElementsByTagName('INPUT');
    alert( ips[0].value);
    alert( ips[1].value);
    alert( ips[2].value);
    alert( ips[3].value);
    /*]]>*/
    </script>
    </body>
    
    </html>
    or
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    
    <body>
    <form name="fred" >
    <input value="000"  />
    <input value="111"  />
    <input value="222"  />
    <input value="333"  />
    </form>
    <script language="JavaScript" type="text/javascript">
    /*<![CDATA[*/
    ips=document.fred.getElementsByTagName('INPUT');
    alert( ips[0].value);
    alert( ips[1].value);
    alert( ips[2].value);
    alert( ips[3].value);
    /*]]>*/
    </script>
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #5
    New Coder
    Join Date
    Feb 2006
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok

    getElementsByTagName('INPUT'); <-- dont think this will work!

    The reason i think this is due to the fact that there are fields in the same form that are not in the array. So id either have to use: "alert (ips[3])" or something else to access the first array box...

    Is there a better way?

    If not this one is ok and I appreciate your help

  • #6
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,518
    Thanks
    3
    Thanked 506 Times in 493 Posts
    forgot what day it was
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    
    <body>
    <input name="rafname[]" value="000"  />
    <input name="rafname[]" value="111"  />
    <input name="rafname[]" value="222"  />
    <input name="rafname1[]" value="313131"  />
    <input name="rafname[]" value="333"  />
    
    <script language="JavaScript" type="text/javascript">
    /*<![CDATA[*/
    ips=document.getElementsByName('rafname[]');
    alert( ips[0].value);
    alert( ips[1].value);
    alert( ips[2].value);
    alert( ips[3].value);
    /*]]>*/
    </script>
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/


  •  

    Posting Permissions

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