Results 1 to 1 of 1

Thread: Problem with login javascript validation

  1. #1
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default Problem with login javascript validation

    Hello everyone,

    I'm using some simple javascript to display an error if they have left either the username or password field empty. Here's the code

    HTML Code:
    <script type="text/javascript">
    function validateForm()
    {
    var x=document.forms["myForm"]["username"].value;
    if (x==null || x=="")
      {
    document.getElementById('boldStuff').innerHTML = 'Username must be filled out';
      return false;
      }
      
      var z=document.forms["myForm"]["pass"].value;
    if (z==null || z=="")
      {
    document.getElementById('boldStuff4').innerHTML = 'Password must be filled out';
      return false;
      }
    }
    </script>
    
    <h1>Login</h1>
    <form action="check.php" onsubmit="return validateForm()" name="myForm" method="post">
      <div class="form_left">
    Username:
      </div>
      <div class="form_right">
    <input name="username" type="text" maxlength="20" />
      </div>
      <div class="clear"></div>
      <div class="form_left">
    Password:
      </div>
      <div class="form_right">
    <input name="pass" type="password" maxlength="20" />
      </div>
      <div class="clear"></div>
      <div class="form_left">&nbsp;</div>
      <div class="form_right">
    <input type="submit" value="Login" />
      </div>
      <div class="clear"></div>
    </form>
    
    
    <span id='boldStuff'></span> 
    <span id='boldStuff4'></span>

    It works fine if they only left the password or the username box empty, but if they leave both empty, it only displays the "Username must be filled out" when I would like it to display both messages. Any help
    Last edited by keyboard; 10-16-2011 at 06:04 AM.

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
  •