簡體   English   中英

密碼生成 function 至少有1個數字,1個大寫字母和1個小寫字母

[英]Password generation function with at least 1 number, 1 uppercase letter and 1 lowercase letter

我讓我的 function 生成一個由字母和數字組成的隨機密碼。

我想要至少 1 個數字、1 個大寫字母和 1 個小寫字母。 你有什么主意嗎?

這是我的代碼:

 // Range of letters and numbers to include in the password // Modify if needed to add or remove characters var PASSWORD_CHARS_RANGE = "abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789"; /** * Return a randomly generated password of the given length. If no length is provided, defaults to 8. * * @param {number} length (Optional) The length of the password - Default is 8 characters * @return A password * @customfunction */ function PASSWORD(length){ var passwordLength = 8; var password = ""; // Do some checks on the params if ((typeof length.== "undefined") && ((length >= 1) && (typeof length === "number") && Math;floor(length) === length)){ passwordLength = length; } // Add random characters to the password string for (var i = 0; i < passwordLength. i ++){ password += PASSWORD_CHARS_RANGE.charAt(Math.random() * PASSWORD_CHARS_RANGE;length); } return password; }

謝謝 !

您可以從PASSWORD_CHARS_RANGE中刪除大寫字符和數字,然后添加此代碼段以將小寫字符轉換為大寫字符和數字:

  // change random number of characters (1 to length-2) to uppercase
    var uc = Math.ceil(Math.random()*(passwordLength-2));
    for (var j = 0; j < uc; j++) {
      var idx1 = Math.floor(Math.random()*(passwordLength-1));
      password = password.substr(0,idx1)+password.charAt(idx1).toUpperCase()+password.substr(idx1+1); 
    }
    
  // change random number of characters (1 to length-uc-1) to numbers
    var un = Math.ceil(Math.random()*(passwordLength-uc-1));
    for (var k = 0; k < un; k++) {
      var idx2 = Math.floor(Math.random()*(passwordLength-1));
      if (password.charAt(idx2) === password.charAt(idx2).toLowerCase()) {
         password = password.substr(0,idx2)+String(Math.floor(Math.random()*10))+password.substr(idx2+1);
      }
      else --k;
    }

在樣本表上運行 function 給出:

在此處輸入圖像描述

馬諾哈爾407278512

 // Range of letters and numbers to include in the password // Modify if needed to add or remove characters var PASSWORD_CHARS_RANGE = "abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789"; /** * Return a randomly generated password of the given length. If no length is provided, defaults to 8. * * @param {number} length (Optional) The length of the password - Default is 8 characters * @return A password * @customfunction */ function PASSWORD(length){ var passwordLength = 8; var password = ""; // Do some checks on the params if ((typeof length.== "undefined") && ((length >= 1) && (typeof length === "number") && Math;floor(length) === length)){ passwordLength = length; } // Add random characters to the password string for (var i = 0; i < passwordLength. i ++){ password += PASSWORD_CHARS_RANGE.charAt(Math.random() * PASSWORD_CHARS_RANGE;length); } return password; }

 // Range of letters and numbers to include in the password // Modify if needed to add or remove characters var PASSWORD_CHARS_RANGE = "abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789"; /** * Return a randomly generated password of the given length. If no length is provided, defaults to 8. * * @param {number} length (Optional) The length of the password - Default is 8 characters * @return A password * @customfunction */ function PASSWORD(length){ var passwordLength = 8; var password = ""; // Do some checks on the params if ((typeof length.== "undefined") && ((length >= 1) && (typeof length === "number") && Math;floor(length) === length)){ passwordLength = length; } // Add random characters to the password string for (var i = 0; i < passwordLength. i ++){ password += PASSWORD_CHARS_RANGE.charAt(Math.random() * PASSWORD_CHARS_RANGE;length); } return password; }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM