![](/img/trans.png)
[英]Regex for “password must contain at least one non-alphanumeric character”
[英]Password should contain at least 1 digit - Regex in ValidateJS
我為此使用 ValidateJS 庫。 這是我正在使用的正則表達式,但它也接受不帶數字的密碼。
我的密碼長度應為 6 到 16 個字符,其中至少包含一位數字。
我希望它只能接受如下密碼:
mypass1
1mypass
my1pass
我在其中至少有 1 個數字。
password: {
presence: true,
length: {
minimum: 6,
maximum: 16
},
format: {
pattern: "^.*(?=.{6,16})(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z0-9!@#$%]+$",
message: "should contain at least one number"
}
},
你可以使用這個模式
^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$
在密碼驗證中,前瞻非常好,請參見以下模式: (?=.{6,16})(?=.*\d).+
它使用兩個積極的前瞻:
(?=.{6,16})
- 保證我們至少有 6 個,最多 16 個字符
(?=.*\d).+
- 確保我們至少有一位數字
更多詳細信息:
.{6, 16}
- 匹配 6 到 16 個任意字符
.*
- 匹配零個或多個任意字符
\d
- 匹配一個數字
.+
- 匹配一個或多個任意字符。
更換這個,它會為你工作
format: {
pattern: "^(?=.{6,16})(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$",
message: "should contain at least one number"
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.