Regular expression to check if password is 8 characters including 1 uppercase letter, 1 special character, alphanumeric characters
The regular expression you are after will most likely be huge and a nightmare to maintain especially for people who are not that familiar with regular expressions.
I think it would be easier to break your regex down and do it one bit at a time. It might take a bit more to do, but I am pretty sure that maintaining it and debugging it would be easier. This would also allow you to provide more directed error messages to your users (other than just Invalid Password
) which should improve user experience.
From what I am seeing you are pretty fluent in regex, so I would presume that giving you the regular expressions to do what you need would be futile.
Seeing your comment, this is how I would go about it:
Must be eight characters Long: You do not need a regex for this. Using the
.Length
property should be enough.Including one uppercase letter: You can use the
[A-Z]+
regular expression. If the string contains at least one upper case letter, this regular expression will yieldtrue
.One special character: You can use either the
\W
which will match any character which is not a letter or a number or else, you can use something like so[!@#]
to specify a custom list of special characters. Note though that characters such as$
,^
,(
and)
are special characters in the regular expression language, so they need to be escaped like so:\$
. So in short, you might use the\W
.Alphanumeric characters: Using the
\w+
should match any letter and number and underscore.
Take a look at this tutorial for more information.
Regex for password of minimum 8 characters, including at least 3 of these: uppercase character, lowercase character, number and special character
I think you better write a for-loop to iterate through the single characters and keep track of which createria has already passed, instead of creating a more and more complex regular expression.
- The regex will be hard to understand / maintained by any programmer in just a few month. And any programmer includes you in a few months
- You could provide detailled information to the user if the requiremens do not match. You could display a message "No uppercase character found in password" etc.
- You could (more) easily implement stuff like "disallow repeating numbers" and so on
- Although performance does not matter, regexp will be way slower than looping.
Regex not working - min 8 character, 1 Number, 1 special character and 1 uppercase letter
KISS - keep it simple.
Is your string length 8 or more? No? Test failed. Yes? Proceed.
Run 4 regex tests on string, one for each parameter.
4 truthy values means you're good to go. Any falsey values means no validation.
Complicated regex will always bite you with weird edge cases if you do not understand or test it thoroughly. Break it into smaller understandable pieces and run the tests consecutively. You want to be correct, not stylish.
Regex password containing two separate digits
About the patterns that you tried
- This pattern
(?=.*\d(?!\d))
matches a digit not directly followed by a digit - This pattern
(?=.*(?<!\d)\d)
matches a digit not directly preceded by a digit
But both patterns do not make sure that there actually are 2 digits
You can assert a digit, at least 1 non digit and then again a digit.
As you don't want to match any of this character class [^\/!?"]
you can omit it from the pattern, and only match the allowed characters, as the .
matches any character.
^(?=.*\d[^\d\n]+\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[-@#$&*+])[-@#$&*+\dA-Za-z]{8,}$
^
Start of string(?=.*\d[^\d\n]+\d)
Assert 2 separate digits(?=.*[a-z])
Assert a char a-z(?=.*[A-Z])
Assert a char A-Z(?=.*[-@#$&*+])
Assert one of the characters that are considered special[-@#$&*+\dA-Za-z]{8,}
Match 8 or more times any of the allowed characters$
End of string
Regex demo
Or a variant using contrast with negated character classes:
^(?=.*\d[^\d\n]+\d)(?=[^a-z\n]*[a-z])(?=[^A-Z\n]*[A-Z])(?=[^-@#$&*+\n]*[-@#$&*+])[-@#$&*+\dA-Za-z]{8,}$
Regex demo
Regex not working - minimum 1 uppercase, minimum 3 lowercase, minimum 1 special character from the defined group (more in description)
You use a quantifier like {1,}
for the lookahead which is not correct.
I think you meant to use the lookaheads like this:
^(?=.{8,12}$)(?=[^A-Z]*[A-Z])(?=\D*\d)(?=(?:[^a-z]*[a-z]){3})(?=[^\s#*.!?$]*[#*.!?$])(?!.*(.)\1)(?:[a-z]|[A-Z])[a-zA-Z0-9#*.!?$,]+$
About the pattern
^
Start of the string(?=.{8,12}$)
Assert lenght 8 - 12(?=[^A-Z]*[A-Z])
Assert an uppercase char(?=\D*\d)
Assert a digit(?=(?:[^a-z]*[a-z]){3})
Assert 3 lowercase chars(?=[^\s#*.!?$]*[#*.!?$])
Assert special char(?!.*(.)\1)
Assert not 2 consecutive chars(?:[a-z]|[A-Z])
Start with upper or lowercase char[a-zA-Z0-9#*.!?$,]+
Match 1+ times any of the listed in the character class$
Assert end of the string
Regex demo | Php demo
Related Topics
Compare Using Thread.Sleep and Timer for Delayed Execution
Fire-And-Forget with Async VS "Old Async Delegate"
Concurrent Hashset<T> in .Net Framework
Xml Serialization - Disable Rendering Root Element of Array
Why Does Casting Int to Invalid Enum Value Not Throw Exception
Regex for Accepting Only Persian Characters
Can a C# Thread Really Cache a Value and Ignore Changes to That Value on Other Threads
How to Access a Control Inside a Xaml Datatemplate
Get All Derived Types of a Type
How to Enable Design Support in a Custom Control
Int.Parse, Input String Was Not in a Correct Format
Should I Use Public Properties and Private Fields or Public Fields for Data
C# Is Rounding Down Divisions by Itself
How to Convert a Gi-Normous Integer (In String Format) to Hex Format? (C#)