•  0
    JavaScript

    Generate random password in Javascript

      Admin     2206        0        Report content

    Generate a random password with 8 chars:

    
    generatePassword(8);
    
    
    function generatePassword(length){
    	//minimum length is 4
    	var chars1 = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
    	var chars2 = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
    	var chars3 = ['1','3','4','5','6','7','8','9','0'];
    	var chars4 = ['!','@','#','$','%','^','&','*','(',')','-','+'];
    	var list = new Array();
    	var password = '';
    	var part = (length - (length % 4))/4;
    	var firstPart = part + (length % 4);
    	for(i = 1; i <= firstPart; i++){
    		index = Math.floor(Math.random() * chars1.length);
    		list.push(chars1[index]);
    	}
    	for(i = 1; i <= part; i++){
    		index = Math.floor(Math.random() * chars2.length);
    		list.push(chars2[index]);
    	}
    	for(i = 1; i <= part; i++){
    		index = Math.floor(Math.random() * chars3.length);
    		list.push(chars3[index]);
    	}
    	for(i = 1; i <= part; i++){
    		index = Math.floor(Math.random() * chars4.length);
    		list.push(chars4[index]);
    	}
    	list = shuffle(list);
    	for(i = 0; i

     

     


  •  


Leave a Comment

Please Login to insert comment.

 

Facebook Conversations