I'm using ValidateJS library for this. 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.
I want it to able to accept passwords only like:
mypass1
1mypass
my1pass
where I have atleast 1 number in it.
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).+
It uses two pisitive lookaheads:
(?=.{6,16})
- assures, that we have at least 6 and at most 16 characters
(?=.*\d).+
- assures that we have at least one digits
Further details:
.{6, 16}
- matches between 6 and 16 of any characters
.*
- matches zero or more of any characters
\d
- match a digit
.+
- 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"
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.