Posted By: Anonymous
I want a regular expression to check that
a password must be eight characters including one uppercase letter, one special character and alphanumeric characters.
And here is my validation expression which is for eight characters including one uppercase letter, one lowercase letter, and one number or special character.
How I can write it for a password that must be eight characters including one uppercase letter, one special character and 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
.Lengthproperty 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 yield
One special character: You can use either the
Wwhich will match any character which is not a letter or a number or else, you can use something like so
[[email protected]#]to specify a custom list of special characters. Note though that characters such as
)are special characters in the regular expression language, so they need to be escaped like so:
$. So in short, you might use the
Alphanumeric characters: Using the
w+should match any letter and number and underscore.
Take a look at this tutorial for more information.