Oleg Krupnik
Oleg Krupnik

Reputation: 45

Regex for password must contain exactly one uppercase character

I want a regular expression to check that:

A password contains exactly one uppercase character, includes at least one special character (#, @, -) and password length must be at least 8 symbols!

(?=.[A-Z]{1}?)(?=.*[#@-])[A-Za-z#@-]{8,}

How can I write it for a password contains exactly one uppercase character?

Upvotes: 2

Views: 1514

Answers (2)

Mohammed Elhag
Mohammed Elhag

Reputation: 4302

Try this:

^(?=(?=.*[@#-]{1,})[\S]{8,})([a-z@#-]*[@#-]*[A-Z][a-z@#-]*[@#-]*)$

https://regex101.com/r/v8Erhu/1

Upvotes: 0

acdcjunior
acdcjunior

Reputation: 135792

Try this one:

^(?=[^A-Z]*[A-Z][^A-Z]*$)(?=.*[#@-])[A-Za-z#@-]{8,}$

Demo: https://regex101.com/r/Fr6znT/1

Breakdown:

  • (?=[^A-Z]*[A-Z][^A-Z]*$) at the position it is means: "The whole expression should be 'any number of non uppercase chars', followed by 'one uppercase char', followed by 'any number of non uppercase chars'".

Upvotes: 1

Related Questions