5,696,038 members and growing! (12,705 online)
Email Password   helpLost your password?
Web Development » Client side scripting » General     Intermediate

Random Password Generator With Pattern String Support

By Prasad Khandekar

A JavaScript based random password generator with pattern string support.
Javascript, HTML, Windows, Visual Studio, IIS 5, IIS 6, IE 6.0, IE 5.5, IE, IIS, Dev

Posted: 4 Oct 2005
Updated: 2 Feb 2006
Views: 24,783
Bookmarked: 22 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
10 votes for this Article.
Popularity: 3.04 Rating: 3.04 out of 5
2 votes, 20.0%
1
0 votes, 0.0%
2
2 votes, 20.0%
3
6 votes, 60.0%
4
0 votes, 0.0%
5

Introduction

Recently, one of my projects required a functionality where upon registering a new user in the system a password for the user had to be generated automatically. The requirement also dictated that the password generation process should support things like:

  • The minimum password length should be configurable.
  • The maximum password length should be configurable.
  • Number of uppercase/lowercase letters appearing should be configurable.
  • Password should contain at least x number of digits and or symbols.
  • Password should be all uppercase or all lowercase or both.

After a little bit of Googling, I came across a very good Password Generator. Unfortunately, this was written in PERL. Since my project was being developed using MS technologies (VB/ASP), I needed a solution using these technologies and that's when I ended up creating this JavaScript function.

Script inputs

The script takes the following four input parameters:

  1. Pattern - This basically defines the password character composition. (Default is autogenerated.)
  2. MinLength - The minimum password length. (Default is 8 characters.)
  3. MaxLength - The maximum password length. (Default is 15 characters.)
  4. AllowDuplicate - Determines, if a character already used can appear subsequently. (Default is False.)

All these parameters are optional. If no values are passed, the script uses the default values. In case of the maximum password length being greater than the pattern string length, the script automatically generates a new pattern string. Actually, this behavior can be altered such that it generates (maxlength - pattern.length) pattern characters only.

Pattern string

The heart of this little script is the pattern string. This string basically contains the following:

  • L - Denotes that a lower case English alphabet is desired at this position.
  • U - Denotes that an upper case English alphabet is desired at this position.
  • 9 - Denotes that a digit between 0-9 is required at this position.
  • S - Denotes that a symbol character is desired at this position.

If the pattern string is not specified the script auto generates one. For this, I have used the Math.random function available in JavaScript. The genPattern function basically generates a random number between 1-4 and decides which pattern character to use based on it. This is repeated till the maximum password length is reached:

function genPattern(pintLen)
{
    var strRet = "";
    var iCntr  = 0;
    var rndNo  = 0;

    for (iCntr = 0; iCntr < pintLen; iCntr++)
    {
        rndNo = Math.floor((4 - 1 + 1) * Math.random() + 1)
        switch (rndNo)
        {
            case 1:
                strRet += "9";
                break;
            case 2:
                strRet += "U";
                break;
            case 3:
                strRet += "S";
                break;
            case 4:
                strRet += "L";
                break;
        }
    }
    return strRet;
}

Password generation

The actual password is generated by calling the getRandomChar(strCharSet, strProcessed, blnAllowDup) function for each pattern character in the pattern string till the maximum password length is reached.

Conclusion

Overall, this is a very simple script and compared to GeodSoft's password generator, offers limited options/flexibility. However it does satisfy the normal requirements. I checked the generated passwords with GeodSoft's Password Evaluator and found out that the password generated by this script is reasonably strong.

Release history

  • 4th Oct, 2005 - First release.
  • 28th Jan, 2006 - Bugs fixed. (Thanks to Mike for pointing out these bugs.)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Prasad Khandekar


I am a software professional with over 15 years of business applications design and development experience. I have started my software carrier way back in 1993 as a COBOL programmer. Later on switched to FoxPro, Magic, Visual Basic, .NET and today I find myself working in Java.
Occupation: Architect
Company: Reflexis Systems (INDIA) Pvt. Ltd.
Location: India India

Other popular Client side scripting articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
Generalfew issues - infinite loopmemberlivetalkvt12:05 26 Jan '06  
AnswerRe: few issues - infinite loopmemberPrasad Khandekar22:38 27 Jan '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 2 Feb 2006
Editor: Rinish Biju
Copyright 2005 by Prasad Khandekar
Everything else Copyright © CodeProject, 1999-2008
Web19 | Advertise on the Code Project