1. Home
  2. Computing & Technology
  3. JavaScript

Password Generator Tutorial

Page Two

<< Previous Page

function getPassword(form) {
var charSet = '';
var chars = [];
var i = 0;

Now we start the password generator function that will be called from the form on the page. The form itself will be passed to the function so that we can easily access all of the input to determine what the criteria for the password will be. We also define an empty character string charset that will contain the list of valid characters to choose from based on which checkboxes are checked and chars is defined as an array to hold the selected characters for our password. Note that I used [] rather than new Array() because the two are equivalent but thwe one I used is much shorter. Finally we define i which will keep track of where we are in the array.

var fnc = form.num.checked;
var flc = form.lower.checked;
var fuc = form.upper.checked;
var foc = form.other.checked;
var ffnc = form.fnum.checked;
var fflc = form.flower.checked;
var ffuc = form.fupper.checked;
var len = form.len.value;

Here I define short names to refer to all of the fields within the form that have been input. This both makes the code shorter to write and also faster to run as the names reference the field values directly.

Next page >>
Explore JavaScript
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. JavaScript

©2009 About.com, a part of The New York Times Company.

All rights reserved.