Posted By

52756 on 02/16/19


Tagged

password random id generate


Versions (?)

Generate Random Passwords or ID's


 / Published in: JavaScript
 

Simple Code to Generate Passwords and Id's

  1. Example call:
  2. passgen( Length, Uppercase , Numbers, Specialchars)
  3. passgen(10, true, true, true) // enables all
  4.  
  5. function passgen(nlength,uchars,numchars,specialchars){
  6.  
  7. var cchars = "abcdefghijklmnopqrstuvwxyz";
  8. var upper_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  9. var nnum = "1234567890";
  10. var cspecial = "#*_$!?%";
  11. var i = 0;
  12. var a = 0;
  13. var new_id = "";
  14.  
  15. if( uchars ){ cchars += upper_chars; }
  16. if( numchars ){ cchars += nnum; }
  17. if( specialchars ){ cchars += cspecial; }
  18. var x = shuffelWord(cchars);
  19.  
  20. while (new_id.length < nlength) {
  21.  
  22. a = Math.floor((Math.random() * cchars.length) + 1);
  23. new_id += cchars.charAt(a);
  24.  
  25. }
  26.  
  27. return new_id;
  28. }
  29. //--------------------------------------------
  30. function shuffelWord (word){
  31. var shuffledWord = '';
  32. word = word.split('');
  33. while (word.length > 0) {
  34. shuffledWord += word.splice(word.length * Math.random() << 0, 1);
  35. }
  36. return shuffledWord;
  37. }

Report this snippet  

You need to login to post a comment.