Problem with simple array loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eDaddi
    New Member
    • Apr 2008
    • 3

    Problem with simple array loop

    I can't figure out why I cant get this simple loop to work. I'm using it to validate a form. I thought I could put the required field names in an array, loop through the array and have the it check each field. My Firebug error states that 'document.user_ reg.check' is invalid. I figure it should read as 'document.user. c_fname.value' for example.

    [CODE=javascript]
    var check=new Array("c_fname" ,"c_lname","c_t itle","c_org"," c_email","c_ema il2","c_phone", "c_password","c _password2");
    for(var i=0; i < check.length;i+ +){
    if(document.use r_reg.check[i].value==null || document.user_r eg.check[i].value==''){
    document.user_r eg.check[i].className='err or';missing++;
    }
    }
    [/CODE]
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Use the form's elements array:

    [code=javascript]document.user_r eg.elements[check[i]]...[/code]

    Comment

    • eDaddi
      New Member
      • Apr 2008
      • 3

      #3
      Well I feel stoopid.

      Thanks Acoder.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        No worries. Glad it helped set you on your way.

        Comment

        Working...