wali
wali

Reputation: 625

Issues in password regular expression

Hi all I am making a password regular expression in javascript test() method, It will take the following inputs

solution

/^(?=.*\d)^(?=.*[!#$%'*+\-/=?^_{}|~])(?=.*[A-Z])(?=.*[a-z])\S{8,15}$/gm

  1. May contains any letter except space

  2. At least 8 characters long but not more the 15 character

  3. Take at least one uppercase and one lowercase letter

  4. Take at least one numeric and one special character

But I am not able to perform below task with (period, dot, fullStop)

(dot, period, full stop) provided that it is not the first or last character, and provided also that it does not appear two or more times consecutively.

Can anyone one help me to sort out this problem, Thanks in advance

Upvotes: 0

Views: 179

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626893

You may move the \S{8,15} part with the $ anchor to the positive lookahead and place it as the first condition (to fail the whole string if it has spaces, or the length is less than 8 or more than 15) and replace that pattern with [^.]+(?:\.[^.]+)* consuming subpattern.

/^(?=\S{8,15}$)(?=.*\d)(?=.*[!#$%'*+\/=?^_{}|~-])(?=.*[A-Z])(?=.*[a-z])[^.]+(?:\.[^.]+)*$/

See the regex demo

Details:

  • ^ - start of string
  • (?=\S{8,15}$) - the first condition that requires the string to have no whitespaces and be of 8 to 15 chars in length
  • (?=.*\d) - there must be a digit after any 0+ chars
  • (?=.*[!#$%'*+\/=?^_{}|~-]) - there must be one symbol from the defined set after any 0+ chars
  • (?=.*[A-Z]) - an uppercase ASCII letter is required
  • (?=.*[a-z]) - a lowercase ASCII letter is required
  • [^.]+(?:\.[^.]+)* - 1+ chars other than ., followed with 0 or more sequences of a . followed with 1 or more chars other than a dot (note that we do not have to add \s into these 2 negated character classes as the first lookahead already prevalidated the whole string, together with its length)
  • $ - end of string.

Upvotes: 1

Related Questions