![](/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.