World changes day by day!

Saturday, June 16, 2012

How to Create Password Generator with JavaScript

1 comment
Tutorial : Javascript
Level : Beginners
Time : 15 minutes

We will create a function that will generate password and alert the password.
function PasswordGenerator()

Declare two variable  password and symbols. We will store the password which going to be generated in the  'password' and in 'symbols' we have different symbols that will strengthen the password which we get generated. You can add more symbols into it.

var password=''";
var symbols="!@#$%&*?";

Now we have the password get generated like below :-

password += String.fromCharCode("A".charCodeAt(0) + myRandom(26));
// characters from big caps letters
password += String.fromCharCode("a".charCodeAt(0) + myRandom(26));
// characters from small caps letters
password += String.fromCharCode("0".charCodeAt(0) + myRandom(10));
// characters from numerals
password += symbols.charAt(myRandom(symbols.length));
// characters from symbols
//12 characters password

We concatenate the passwords with '+', String is used to get the strings. Next,  we use the fromCharCode()  method, which converts Unicode values into characters. The charAt() method returns the character at the specified index in a string. The function 'myRandom' randomises, here we used 26 for the alphabets, 10 the 0-9 numerals, for  symbols, we use the length property to get the numbers of symbols use while initialization.

 Since the password we have is simple, might not be good so we will shuffle it, all the characters so we have strengthen the password. To give more and more strength we shuffles it 3-5 time.

password = shuffleString(password);
password = shuffleString(password);
password = shuffleString(password); //shuffled the password 3 time to provide strength
alert(password); //alert the password

Time to write the code for 'shuffleString' function used above. 

function shuffleString(mystr)
var arr=mystr.split('');

for(i=0;i< mystr.length;i++)
var r1= i;
var r2= myRandom (mystr.length);

var tmp = arr[r1];
arr[r1] = arr[r2];
arr[r2] = tmp;

return arrPwd.join("");

The split() method is used to split a string into an array of substrings. Here we have used the  empty string ("") which act as the separator, the string is split between each characters. All the characters that got splitted stored in the array called 'arr'. We follow the simple logic to shuffles, first we store the iteration value in variable r1 and the random number of the string length in variable r2, then swap the arrays of those index.

function  myRandom (val) 
return (Math.floor(Math.random() * val));

Above 'myRandom' function is quite easily understood, we have used the math 'random' object method , which return number between 0 and 1, which got multiplied by the 'val', passes as argument. To get perfect integers, used math object method 'floor', that rounded downwards to the nearest integer.

That all, we finished the things, now put all of them together and have your password generator.
Download Code
Thanks for reading this JavaScript tutorial, Put your comment to let me know how you worked out on it.

1 comment :

Leave Your comments