1. Home
  2. Computing & Technology
  3. JavaScript

Password Generator Tutorial

Page Three

Join the Discussion

Questions? Comments?

<< Previous Page

if (!fnc && !flc && !fuc && !foc) {
form.num.checked = form.lower.checked = form.upper.checked = 1;
alert('You must allow some type of characters');
return;
}
if (!ffnc && !fflc && !ffuc) {
form.flower.checked = form.fupper.checked = 1;
alert('You must allow a start character');
return;
}

This code validates that at least one checkbox in each of the two groups is selected and produces an alert popup with an error message if either group is empty. Note that I can reference the short named fields that I copied the form fields to to test if they are all not checked but have to use the original names to actually update the form fields back to their original values. I assign 1 to the fields that I want checked rather than true because the two values are equivalent and 1 is shorter. I have also combined the assignment statements together to make the code shorter as well. In each case we end with a return statement since if the input is not valid we don't want to even try to generate a password.

if (len != parseInt(len) || len < 7 || len > 20) {
form.len.value = 8;
alert('Please generate a password between 7 and 20
continued from previous line characters long');
return;
}

Here I validate the length that has been entered. First I quickly test it for numeric and then test that the value is between 7 and 20. Because Javascript only looks at the code following an || (or) condition if the prior condition is true the tests for less than 7 and greater than 20 will only be performed if the value is numeric.

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.