Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


May 21, 2009

Random Passwords on Demand

HTA makes it easy to generate random passwords
RSS
Subscribe to Windows IT Pro | See More Jscript Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

Listing 2: The generate Function

// Executes when the Generate Passwords button is clicked.
function generate() {
  var chars, len, num, m, n, pwds, pwd;

  // Build a string based on the check boxes.
  chars = "";
  if (id("uppercase").checked) chars += UPPER_CHARS;
  if (id("lowercase").checked) chars += LOWER_CHARS;
  if (id("numbers").checked)   chars += NUMBER_CHARS;
  if (id("symbols").checked)   chars += SYMBOL_CHARS;

  // Complain if none of the check boxes are selected.
  if (chars == "") {
    window.alert("Select at least one character class.");
    id("uppercase").focus();
    event.returnValue = false;
    return;
  }

  // Parse the password length field as base 10 number. Complain
  // if the value is not a number or outside the allowed range.
  len = parseInt(id("len").value, 10);
  if (isNaN(len) || (len < 1) || (len > MAX_PASSWORD_LENGTH)) {
    window.alert("Enter a number in the range 1 to " +
      MAX_PASSWORD_LENGTH + ".");
    event.returnValue = false;
    id("len").focus();
    return;
  }

  // Parse the number of passwords field as a base 10 number. Complain
  // if the value is not a number or outside the allowed range.
  num = parseInt(id("num").value, 10);
  if (isNaN(num) || (num < 1) || (num > MAX_PASSWORD_NUMBER)) {
    window.alert("Enter number in the range 1 to " +
      MAX_PASSWORD_NUMBER + ".");
    event.returnValue = false;
    id("num").focus();
    return;
  }

  // Clear the list of passwords.
  id("passwords").value = "";

  // The pwds variable will contain a newline-delimited list of
  // random passwords. The code generates a random password, stores
  // it in the pwd variable, and appends it to the pwds variable.
  pwds = "";
  for (m = 0; m < num; m++) {

// Begin Callout A
    pwd = "";
    for (n = 0; n < len; n++)
      pwd += chars.charAt(Math.floor(Math.random() * chars.length));
// End Callout A

// Begin Callout B
    pwds += pwds == "" ? pwd : "\n" + pwd;
// End Callout B
  }

  // Update the password list and enable the Copy to Clipboard and
  // Clear List buttons.

// Begin Callout C
  id("passwords").value = pwds;
  id("copydata").disabled = false;
  id("clear").disabled = false;
// End Callout C
}

End of Article

   Previous  1  2  [3]  Next  


Reader Comments

You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
WinInfo Short Takes: Week of November 23, 2009

An often irreverent look at some of the week's other news, including some post-PDC some soul searching, a Google Chrome OS announcement and a Microsoft response, Windows 7 off to a supposedly strong start, the Jonas Brothers and Xbox 360, and so much more ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

2009 Windows IT Pro Editors' Best and Community Choice Awards

Picking a favorite product from an impressive crowd of competitive offerings is never an easy task, and such was the case with our Editors' Best and Community Choice awards this year. ...


Related Articles HTA Automatically Creates Mappings

5 Tips to Ensure Safe Passwords

Turn Digital Objects Into Passwords

HTA F/X

Security Whitepapers Reducing the Costs and Risks of Branch Office Data Protection

Solving Desktop Management Challenges in Healthcare

Solving Desktop Management Challenges in Education

Related Events Deep Dive into Windows Server 2008 R2 presented by John Savill

Introduction to Identity Lifecycle Manager "2"

SQL Server Security: How to Secure, Monitor & Audit Your Databases

Check out our list of Free Email Newsletters!

Security eBooks Spam Fighting and Email Security for the 21st Century

Understanding and Leveraging Code Signing Technologies

A Guide to Windows Certification and Public Keys

Related Security Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement