简体   繁体   English

密码应至少包含 1 位数字 - ValidateJS 中的正则表达式

[英]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. .+ - 匹配一个或多个任意字符。

Demo演示

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.

相关问题 正则表达式“密码必须至少包含一个非字母数字字符” - Regex for “password must contain at least one non-alphanumeric character” 密码正则表达式:必须以大写字母开头且仅包含一位数字 - Regex for password: Must start with a capital letter and contain only one digit 密码的正则表达式必须至少包含八个字符,至少一个数字以及大小写字母和特殊字符 - Regex for password must contain at least eight characters, at least one number and both lower and uppercase letters and special characters 最少 6 个字符的密码 REGEX,至少一个字母和一个数字,可以包含特殊字符 - Password REGEX with min 6 chars, at least one letter and one number and may contain special characters 正则表达式至少包含一个字符。 成对的双引号。 不应包含@和? - Regex containing at least a character. Double quotes in pair. Should not contain @ and? 六个字符的正则表达式,至少有一位 - Regex for six characters with at least one digit 匹配每个 n 位的正则表达式至少有一个数字 1 - Regex to match each n digit has at least one digit 1 validatejs确认react-native的密码 - validatejs confirm password for react-native 密码正则表达式 - 最少 12 个字符,应包含 1 个大写、小写、数字和特殊字符,并且不应包含超过 2 个重复字符 - Password RegEx - Minimum 12 char, Should contain 1 Uppercase & Lowercase & Number & Special Char and should not contain more than 2 repeated char 正则表达式至少包含 1 个特殊字符但不包含特定字符 - Regex to contain at least 1 special character but not specific characters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM