[英]Password should contain at least 1 digit - Regex in ValidateJS
I'm using ValidateJS library for this.我为此使用 ValidateJS 库。 Here is the regex that I'm using but it does accept password without digits as well.
这是我正在使用的正则表达式,但它也接受不带数字的密码。
My password should be with length 6 to 16 characters, at least one digit in it.我的密码长度应为 6 到 16 个字符,其中至少包含一位数字。
I want it to able to accept passwords only like:我希望它只能接受如下密码:
mypass1
1mypass
my1pass
where I have atleast 1 number in it.我在其中至少有 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"
}
},
you can use this pattern你可以使用这个模式
^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$
In password validation, lookaheads are great, see this pattern: (?=.{6,16})(?=.*\d).+
在密码验证中,前瞻非常好,请参见以下模式:
(?=.{6,16})(?=.*\d).+
It uses two pisitive lookaheads:它使用两个积极的前瞻:
(?=.{6,16})
- assures, that we have at least 6 and at most 16 characters (?=.{6,16})
- 保证我们至少有 6 个,最多 16 个字符
(?=.*\d).+
- assures that we have at least one digits (?=.*\d).+
- 确保我们至少有一位数字
Further details:更多详细信息:
.{6, 16}
- matches between 6 and 16 of any characters .{6, 16}
- 匹配 6 到 16 个任意字符
.*
- matches zero or more of any characters .*
- 匹配零个或多个任意字符
\d
- match a digit \d
- 匹配一个数字
.+
- matches one or more of any characters. .+
- 匹配一个或多个任意字符。
Replace this and it will work for you更换这个,它会为你工作
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.