JavaScriptを使ったランダムなパスワード文字列を生成するサンプルコードです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /// length 桁のパスワードを生成する function createRandomPassword(length){ var lc = "abcdefghijklmnopqrstuvwxyz" ; var sList = lc + lc.toUpperCase()+ "0123456789_-" ; // 小文字 + 大文字 + 数字 + 対応する記号 var password = "" ; for (i = 0; i < length; i++) password += sList.charAt(Math.floor(Math.random() * sList.length)); return password; } // 利用例 var passwordLength = 12; var myPassword = createRandomPassword(passwordLength); console.log(myPassword); prompt(passwordLength + "桁のパスワードを生成しました。" , myPassword); |
デモ:
桁数:
ボタンを押すたびランダムなパスワード文字列を作成します。
ポチポチ押してみてくださいね。