简体   繁体   English

JavaScript密码生成器创建奇怪的字符

[英]JavaScript password generator creating strange character

I have created a simple password generator using javascript.我使用 javascript 创建了一个简单的密码生成器。

When you click the button on the page, it creates you a password and displays it below.当您单击页面上的按钮时,它会为您创建一个密码并将其显示在下方。 This password generator creates a string containing the following:此密码生成器创建一个包含以下内容的字符串:

8 random letters from az.来自 az 的 8 个随机字母。

2 random numbers from 0-9. 0-9 的 2 个随机数。

1 random special character from an array of strings.字符串数组中的 1 个随机特殊字符。 (see array 'chars'). (参见数组“字符”)。

However, after you click the button a few times, eventually, a password will be generated with 2 special characters, with a 'Â' in it.但是,在您单击按钮几次后,最终会生成一个包含 2 个特殊字符的密码,其中包含一个“”。 I'm confused because this character isnt in the chars array.我很困惑,因为这个字符不在 chars 数组中。

Why is this, and is there anyway to tell the JS to not include that character in particular?为什么会这样,有没有告诉 JS 特别不要包含那个字符?

 function myFunction() { // create initial arrays. a = []; var chars = ['#', '%', '£', '!', '?', '&']; for (var i = 97; i <= 122; i++) { a[a.length] = String.fromCharCode(i).toUpperCase(); // create random letters. var one = a[Math.floor(Math.random() * a.length)]; var two = a[Math.floor(Math.random() * a.length)]; var three = a[Math.floor(Math.random() * a.length)]; var four = a[Math.floor(Math.random() * a.length)]; var five = a[Math.floor(Math.random() * a.length)]; var six = a[Math.floor(Math.random() * a.length)]; var seven = a[Math.floor(Math.random() * a.length)]; var eight = a[Math.floor(Math.random() * a.length)]; // create random numbers. var int1 = Math.floor(Math.random() * 10); var int2 = Math.floor(Math.random() * 10); var ints = int1.toFixed(0) + int2.toFixed(0); var intsDecimal = int1.toFixed(0) + "." + int2.toFixed(0); // create random characters, based on array (chars). var randChar = chars[Math.floor(Math.random() * chars.length).toFixed(0)]; // create variable moving all letters, numbers and characters together. var c = one + two + three + four + five + six + seven + eight + ints + randChar; } // display variable c. document.getElementById('userPass').innerHTML = c; }
 <p>Using simple JS to create a random password.</p> <button onclick="myFunction()">CLICK</button> <h4>Your password is:</h4> <h3 id="userPass"></h3>

Generate a password using a specific pattern like 3 UpperCase, 3 lowercase, special char, and digit all lengths as you like.使用特定模式生成密码,如 3 个大写字母、3 个小写字母、特殊字符和任意长度的数字。

const generatePassword = (str, limit) =>{
    let password = "";
        for (let i = 1; i <= limit; i++) {
               let char = Math.floor(Math.random()
                           * str.length);
               password += str.charAt(char)
           }
      return password;
}

let password = () => {
           let ucstr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
            let lcstr = 'abcdefghijklmnopqrstuvwxyz';
            let digitstr = '0123456789';
            let spstr = '@#$';
            
           const password = generatePassword(ucstr, 5) + generatePassword(lcstr, 3)  + generatePassword(digitstr, 2)  + generatePassword(spstr, 2);
           return password;
       }
console.log("Your Password us ===> ", password());

Sample Output is AXTSFadsf34#@样本输出为AXTSFadsf34#@

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM