user and Password validation script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lito
    New Member
    • Aug 2014
    • 3

    user and Password validation script

    I'm writing a password validation script and I found the script below, it works fine but I want it to validate apassword with exact 8 characters tried different solutions but can't get it work, can you help please?

    Code:
    <script type="text/javascript">
    
      function checkForm(form)
      {
        if(form.username.value == "") {
          alert("Error: Username cannot be blank!");
          form.username.focus();
          return false;
        }
        re = /^\w+$/;
        if(!re.test(form.username.value)) {
          alert("Error: Username must contain only letters, numbers and underscores!");
          form.username.focus();
          return false;
        }
        
        if(form.pwd1.value != "" && form.pwd1.value == form.pwd2.value) {
             if(form.pwd1.value.length < 6){
            alert("Error: Password must contain at least six characters!");
            form.pwd1.focus();
            return false;
          }
          if(form.pwd1.value == form.username.value) {
            alert("Error: Password must be different from Username!");
            form.pwd1.focus();
            return false;
          }
          re = /[0-9]/;
          if(!re.test(form.pwd1.value)) {
            alert("Error: password must contain at least one number (0-9)!");
            form.pwd1.focus();
            return false;
          }
          re = /[a-z]/;
          if(!re.test(form.pwd1.value)) {
            alert("Error: password must contain at least one lowercase letter (a-z)!");
            form.pwd1.focus();
            return false;
          }
          re = /[A-Z]/;
          if(!re.test(form.pwd1.value)) {
            alert("Error: password must contain at least one uppercase letter (A-Z)!");
            form.pwd1.focus();
            return false;
          }
        } else {
          alert("Error: Please check that you've entered and confirmed your password!");
          form.pwd1.focus();
          return false;
        }
    
        alert("You entered a valid password: " + form.pwd1.value);
        return true;
      }
    
    </script> <form ... onsubmit="return checkForm(this);"> <p>Username: <input type="text" name="username"></p> <p>Password: <input type="password" name="pwd1"></p> <p>Confirm Password: <input type="password" name="pwd2"></p> <p><input type="submit"></p> </form>
    Last edited by Rabbit; Aug 15 '14, 03:29 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    I see in your code that you check for at least 6 characters, I'm not sure what trouble you are having checking for 8.

    Comment

    • lito
      New Member
      • Aug 2014
      • 3

      #3
      Thank you Rabbit for your prompt reply to my query.
      my trouble is that I want to validate exact number of characters, not less, no more than 6 but exactly six characters.

      Comment

      • Exequiel
        Contributor
        • Jul 2012
        • 288

        #4
        you can do it this way.
        Code:
        if(form.pwd1.value.length == 6)

        Comment

        • lito
          New Member
          • Aug 2014
          • 3

          #5
          Thanks Exequiel, I'm On my way! Way to work and will let you know after trying.
          Thanks

          Comment

          Working...