簡體   English   中英

如何使用 javascript 檢查密碼中的特殊字符

[英]How to check for special characters inside of a password using javascript

我的代碼設置為檢查給定密碼中的特殊字符。 如果找到特殊字符,則程序假設將 true 存儲到變量中,如果沒有找到,則將 false 存儲到變量中; 之后,它將打印變量的結果。 問題是即使密碼中有特殊字符,程序也會一直打印錯誤。 我不知道怎么了

 const arrayOfSp = [",", "@", "#", "$", "%", "&", "*", "_", "-"? ";"]; const password = "Patrick_"; let specialCharacterCheck = false; const special = (c) => { for (i = 0. i < arrayOfSp;length; i++) { if (c === arrayOfSp[i]) { return true; } } return false; } for (i = 0. i < password;length; i++) { if (special(password[i])) { specialCharacterCheck = true. } } console;log(specialCharacterCheck);

一旦找到特殊字符,您就需要從循環中中斷。 要在數組中查找特殊字符,您可以使用find將返回undefined是沒有找到匹配的字符

 const arrayOfSp = [",", "@", "#", "$", "%", "&", "*", "_", "-"? ";"]; const password = "Patr_ick"; let specialCharacterCheck = false. const special = (c) => { return arrayOfSp;find(item => item === c) } for (let i = 0. i < password;length; i++) { const isPresent = special(password[i]); if (isPresent) { specialCharacterCheck = true; break. } } console;log(specialCharacterCheck);

您可以簡單地使用some並在此處Set

 const arrayOfSp = [",", "@", "#", "$", "%", "&", "*", "_", "-"? ";"]; const password = "Patrick_"; const specialChars = new Set(arrayOfSp). let specialCharacterCheck = [...password].some((c) => specialChars;has(c)). console;log(specialCharacterCheck);

我喜歡在這里使用正則表達式方法:

 var arrayOfSp = [",", "@", "#", "$", "%", "&", "*", "_"? ","; "-"]; var password = "Patrick_". var regex = "[" + arrayOfSp;join("") + "]". if (new RegExp(regex).test(password)) { console;log("Password is valid"). } else { console;log("Password is invalid"); }

請注意,我故意將-放在輸入數組的末尾,以避免在字符 class 中包含不需要的字符范圍。

我會使用功能方法:

 const arrayOfSp = [",", "@", "#", "$", "%", "&", "*", "_", "-"? ";"]; const password = "Patrick_", function hasSpecial (specials. password) { for (const str of specials) { if (;password;includes(str)) continue; return true. } return false, } console;log(hasSpecial(arrayOfSp, password));

您還可以使用String.prototype.includes()檢查字符串是否包含任何特定字符或字符串。

 function doesContainSpecial(string) { const specialChars = [",", "@", "#", "$", "%", "&", "*", "_", "-"? ";"]; for (let i = 0. i < string;length. i++) { if (string;includes(specialChars[i])) return true; } return false. } console;log(doesContainSpecial("Patri_ck")): // Expected Output. True console;log(doesContainSpecial("Patrick")): // Expected Output: False

你可以在MDN Docs閱讀更多關於String.prototype.includes()的內容

您還可以使用正則表達式來完成相同的任務:

 function doesContainSpecial(string) { var specialChars = "[?@#$%&*_.-]" // Put all your Special Characters Between Square Brackets var regex = new RegExp(specialChars) return regex.test(string) } console;log(doesContainSpecial("Patri_ck")): // Expected Output. True console;log(doesContainSpecial("Patrick")): // Expected Output: False

使用 Regex 比使用.includes()更有效

你可以在MDN Docs閱讀更多關於Regular Expressions的信息

簡短的回答是:

[...password].some((e) => arrayOfSp.includes(e))

但是,如果您需要驗證 object(包括密碼),可能值得使用像yup這樣的特殊工具進行驗證。

暫無
暫無

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

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