Reputation: 19
I'm attempting to use the format: pattern: functionality of Validate.js to make sure a password meets the requirements of at least one upper case, one lower case, one number, and one special character, with a minimum length of 8 and a max of 20. The presence and length function as intended. Anyone got any tips?
password: {
presence: {
allowEmpty: false,
message: "is required",
admin: "required",
label: "Password*",
},
length: {
minimum: 8,
message: "must be at least 8 characters",
maximum: 20,
},
format: {
pattern: /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!|@|#|\$|%|\^|&|\*])(?=.{8,20})/,
}
},
Upvotes: 1
Views: 365
Reputation: 18611
Use
/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*]).{8,20}$/
EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
[a-z] any character of: 'a' to 'z'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
[A-Z] any character of: 'A' to 'Z'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
[!@#$%^&*] any character of: '!', '@', '#', '$',
'%', '^', '&', '*'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
.{8,20} any character except \n (between 8 and 20
times (matching the most amount possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
Upvotes: 2