title: Login form validation using javascript and php
body: <p>I have been trying to create a login form using JavaScript. My login form needs to do the following using JavaScript validation</p>
  
  <ol>
  <li>login field contains only letters with a minimum of 4 and a maximum of 6 letters</li>
  <li>password is at least 5 characters long and not more than 7 characters and contains only letters and digits with at least 1 letter and at least 1 digit</li>
  <li>if the data is incorrect, generate an alert window with an appropriate
  message and set the cursor to the incorrect input field and the form needs to be submitted to a php file which needs to validate against a text file</li>
  </ol>
  
  <p>I tried to use regular expression to check for the login fields and password in the javascript and  i have used a login .php but haven't done anything in that till now.However my JavaScript/HTML file which i have pasted below doesn't seem to work. Can anyone tell me the issue with my file?</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;script&gt;
  function validateFormOnSubmit(theForm) {
  
    reason += validateUsername(theForm.username.value)
    reason += validatePassword (theForm.pwd.value)
    if (reason == "") return true 
   else { alert(reason); return false }
  
  }
  
  function validateUsername(fld) {
     if (fld == "")
        return "No username entered"
  
     else if((fld.length &gt; 6) || (fld.length &lt; 4 ))
        return "value must be between 4 and 6 characters"
  
    else if (/[^a-zA-z]/.test(fld))
         return "only  letters  allowed"
  
  }
  function validatePassword (fld){
  if (fld == ""){
     return "No password entered";
  }
  else if (fld.length&lt;5 ||fld.length&gt;7 ) {
   return "length shoild be b/w 4 and 7";
  }
  else if (!/[a-z]/.test(fld) || !/[A-Z]/.test(fld) || !/[0-9]/.test(fld)){
      return "passwords rewuire one of the letters or digitys";
  }
  }
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;table class ="signup" border ="o" cellpadding ="2"
   cellspacing ="5" bgcolor ="eeeeee"&gt;
  &lt;th colspan="2" align="center"&gt;SignupForm&lt;/th&gt;
  &lt;form method = "post" action ="login.php" onSubmit="return validateFormOnSubmit(this)"&gt;
   &lt;tr&gt;  
  &lt;td&gt;Your user name:&lt;/td&gt;
  &lt;td&gt;&lt;input name = "username" size="35" maxlength="50" type="text"&gt;&lt;/td&gt;
   &lt;/tr&gt;
   &lt;tr&gt; 
   &lt;td&gt;Your Password:&lt;/td&gt;  
   &lt;td&gt;&lt;input name="pwd" size="35" maxlength="25" type="password"&gt;&lt;/td&gt;
  &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&amp;nbsp;&lt;/td&gt;
      &lt;td&gt;&lt;input name="Submit" value="Send" type="submit" &gt;&lt;/td&gt;
      &lt;td&gt;&amp;nbsp;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/form&gt;
  &lt;/table&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>