www.webdeveloper.com
Results 1 to 5 of 5

Thread: Checking length of form field

  1. #1
    Join Date
    Jan 2003
    Location
    UK
    Posts
    267

    Checking length of form field

    Hi,

    I'm trying to change the border colour of any text input form field that has a length of more than zero. So far I've been trying this but it doesn't work...

    Code:
    <script type="text/javascript">
    var notempty = document.getElementsByTagName(input).value;
    if (notempty.value.length > 0) { document.getElementsByTagName(input).style.borderColor="red"; }
    else { document.getElementsByTagName(input).style.borderColor="transparent"; }
    </script>
    I don't know if it's a syntax thing, or if I'm just plain doing it wrong.

    I'm trying to actually learn and understand Javascript so if you could help me to adapt this code rather than giving me a more efficient way of doing this then that'd be great.

    cheers guys

  2. #2
    Join Date
    Mar 2011
    Posts
    1,138
    Try:
    Code:
    <script type="text/javascript">
    var notempty = document.getElementsByTagName('input');  // Get all of the <input> tags in the array 'notempty'
    for (i=0; i<notempty.length; i++) {  // Loop through the entire array of <input> tags
      if (notempty[i].type == 'text') {  // Only check type="text" <input> tags
        if (notempty[i].value.length > 0) { notempty[i].style.borderColor="red"; }
         else { notempty[i].style.borderColor="transparent"; }
      } // endif type='text'
     } // end for i
    </script>
    Rick Trethewey
    Rainbo Design

  3. #3
    Join Date
    Jan 2003
    Location
    UK
    Posts
    267
    Hi sorry it's not working. I tried to understand the code - does 'i' stand for integer?

    I don't understand why my original code didn't work - it seems logically set out. odd...

  4. #4
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    I don't understand why my original code didn't work
    Use the error console.

    Quote Originally Posted by W8 4me View Post
    Hi sorry it's not working.
    Such a block of inline code must me positioned below the elements to which it refers.
    Last edited by Logic Ali; 07-07-2012 at 06:56 PM.
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  5. #5
    Join Date
    Jan 2003
    Location
    UK
    Posts
    267
    Hi thanks.

    I put rtrethewey's code in a function called checkme() and then called it in the input tag as an onkeyup.

    rtrethewey, I've just learnt about loops on codecademy.com, but was wondering why we need a loop for this?

    Also why won't the script work without needing to be in a function/called from the onkeyup?

    (i=0; i<notempty.length; i++) - please can you explain this line? I've got loops sort of in my head, but using numbers.

    thanks again

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles