Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts

    alphanumeric password validation problem

    hello,
    I'm trying to do alphanumeric password validation,

    I've found a script on another forum, but I've having issues with one part of it.

    this:
    PHP Code:
        var sizechar 5;//length for password

        
    var alph_valid="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

        
    for (var i=0i<sizechari++) {
            if (
    alph_valid.indexOf(upass_string.charAt(i)) < 0) {
                
    alphaCount++
            }
        } 
    part seems to be a problem area.

    If i use a password like "123456" it seems to be counting all of these as characters. and I don't know even about javascript to solve the problem.

    any help would be great,

    here's the entire script:

    PHP Code:
    <script>
    function 
    ValidateForm(){

        var 
    sizechar 5;//length for password
        
    var upassID=document.PasswordTest.password;

        
    //check if passsword is filled out
        
    if ((upassID.value==null)||(upassID.value=="")){
            
    alert("Please enter your password");
            
    upassID.focus();
            return 
    false;
        }
        
    //add regex alphanumeric check here
        
    if (upassID.length sizechar) {
            
    alert('Your password is too long');
            
    upassID.focus();
            return 
    false;
        }
        if (
    upassID.length sizechar) {
            
    alert('Your password is too short');
            
    upassID.focus();
            return 
    false;
        }
            var 
    upass_string upassID.value;

        var 
    valid="123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

        for (var 
    i=0i<sizechari++) {
            if (
    valid.indexOf(upass_string.charAt(i)) < 0) {
                
    alert('Your password contains invalid characters');
                
    upassID.focus();
                return 
    false;
            }
        }

        var 
    alphaCount 0;
        var 
    numCount 0;
        var 
    num_valid="0123456789";

        for (var 
    i=0i<sizechari++) {
            if(
    num_valid.indexOf(upass_string.charAt(i)) < 0) {
             
    numCount++
            }
        }
        if(
    numCount==upass_string.length){
            
    alert('Your password contains only characters. Please enter an alphanumeric value like -alpha1-');
            
    upassID.focus();
            return 
    false;
        }

        var 
    alph_valid="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

        
    for (var i=0i<sizechari++) {
            if (
    alph_valid.indexOf(upass_string.charAt(i)) < 0) {
                
    alphaCount++
            }
        }
        
    alert("Alpha Counter: " alphaCount);
        if(
    alphaCount==upass_string.length){
            
    alert('Your password contains only numbers. Please enter an alphanumeric value like -alpha1-');
            
    upassID.focus();
            return 
    false;
        }
        return 
    true;
     }
    </script>

    <form name='PasswordTest' id='PasswordTest' >
    <input type='text' name='password' id='password' />
    <input type='button' value='check password' onclick='return ValidateForm();' />
    </form> 

    thanks in advance for your time!
    -Ken

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    My undersatnding is that he wants a password of exactly five letters, ignoring case, and no numbers or other characters.

    function validate(nField){

    var userInput = nField.value;
    if (!/^[A-z]{5}$/.test(userInput))
    {
    alert('Only five letters are allowed');
    nField.value = "";
    nField.focus();
    }
    }

    Obviously {5} can be changed to some other avlue if required, or say {5,8} to permit min of 5 and max of 8 character.

  • #3
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    hello,
    thanks for your replies!

    but i actually do want "alphanumeric" passwords,
    so I would like it to continue with atleast one letter and one number, with a minimum of 5 characters length.

    also like i mentioned above,
    this seems to be the problem area:

    PHP Code:
        for (var i=0i<sizechari++) { 
            if (
    alph_valid.indexOf(upass_string.charAt(i)) < 0) { 
                
    alphaCount++ 
            } 
        } 
    it seems to be counting numbers as letters, and i don't understand why?

    thanks again for all your help,
    -Ken

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    "but i actually do want "alphanumeric" passwords,
    so I would like it to continue with at least one letter and one number, with a minimum of 5 characters length."

    That is not what you asked for at first.

    Try this:-

    function validate(nField) {

    var userInput = nField.value;

    if ((userInput.length>=5) && (/!\W/.test.userInput)) { /// minimum of 5 alphanumeric characters only
    if (userInput.match (/\d/g)!=null && userInput.match(/\d/g).length>=1 && userInput.match (/[a-z]/gi)!=null && userinput.match (/[a-z]/gi).length>=1 ) { /// at least one letter (ignoring case) and one number
    alert ("Password is valid")
    }
    }
    else {
    alert ("Password is not valid")
    }
    }


    Your other script should be

    for (var i=0; i<sizechar; i++) {
    if (alph_valid.indexOf(upass_string.charAt(i)) !=-1) {
    alphaCount++ // i.e. character is found
    }
    }
    Last edited by Philip M; 12-20-2006 at 06:00 PM. Reason: Typo

  • #5
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I was testing it out for my own purpose and found a typo so figured I'd repost Philip's code in my test case in case someone was having trouble with it.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
      <head>
        <title>Password Validation</title>
        <script type="text/javascript">
          function doIt(s) {
            var adiv = document.getElementById("adiv");
            adiv.innerHTML = s;
          }
    
          function validate(nField) {
            var userInput = nField.value;
    
            if ((userInput.length>=5) && (!/\W/.test(userInput)) /// minimum of 5 alphanumeric characters only
                                      && userInput.match(/\d/g) != null 
                                      && userInput.match(/\d/g).length >= 1 /// at least one number
                                      && userInput.match(/[a-z]/gi) != null 
                                      && userInput.match(/[a-z]/gi).length >= 1 ) { /// at least one letter (ignoring case)
              doIt("Password is valid");
            } else {
              doIt("Password is not valid");
            }
          }
        </script>
      </head>
      <body>
        <div>
          <input type="text" name="pw" id="pw" />
          <button onclick="validate(document.getElementById('pw'));">Validate It</button>
          <br />
          <div id="adiv"> </div>
        </div>
      </body>
    </html>
    david_kw


  •  

    Posting Permissions

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