Regex to validate password - regex

I've looked on here for some ideas but I still seem to be struggling with coming up with a regular expression to meet my requirements.
I need a regular expression to check a password format, the criteria are:
At least 1 uppercase letter
At least 1 number
Only alphanumeric characters (no special characters)
At least 8 characters long
The regular expression I'm using is:
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$
However this is also allowing characters like !$&.
Is there a modification I need to make to this to get it to stop these special characters being accepted?

Change the last part .{8,} to [a-zA-Z\d]{8,}
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$

Related

Regular Expression for Password strength with one special characters except Underscore

I have the following regular expression:
^.*(?=^.{8,}$)(?=.*\d)(?=.*[!##$%^&*-])(?=.*[A-Z])(?=.*[a-z]).*$
I am using it to validate for
At least one letter
least one capital letter
least one number
least one special characters
least 8 characters
But along with this I need to restrict the underscore (_).
If I enter password Pa$sw0rd, this is validating correctly, which is true.
If I enter Pa$_sw0rd this is also validating correctly, which is wrong.
The thing is the regex is passing when all the rules are satisfied. I want a rule to restrict underscore along with above.
Any help will be very appreciable.
I think you can use a negated character class [^_]* to add this restriction (also, remove the initial .*, it is redundant, and the first look-ahead is already at the beginning of the pattern, no need to duplicate ^, and it is totally redundant since the total length limit can be checked at the end):
^(?=.*\d)(?=.*[!##$%^&*-])(?=.*[A-Z])(?=.*[a-z])[^_]{8,}$
See demo
^(?=.*?\d)(?=.*?[!##$%^&*-])(?=.*?[A-Z])(?=.*?[a-z])(?!.*_).{8,}$
You can try this..* at start is of no use.See demo.
https://regex101.com/r/pG1kU1/34

MVC .net password validation using regular expression

I'm writing a regular expression to validate a password. The conditions are:
Password must contain at least two special characters
Password must be at least eight characters long
Password must be alpha numeric
I'm able to make sure that there are atleast 8 characters, atleast one alphabet, atleast one number and atleast one special character using the below Regular expression:
(?=.*[A-z])(?=.*[0-9])(?=.*?[!##$%\^&*\(\)\-_+=;:'""\/\[\]{},.<>|`]).{8,32}
Only condition i'm not able to get is there must be atleast two special characters (Above Reg exp is atleast one special characters). Does anyone have any idea on this?
Thanks in advance.
Only condition i'm not able to get is there must be atleast two special characters.
Make it twice by putting the pattern which was present inside the lookahead inside a group and then make it to repeat exactly two times.
^(?=.*[A-Za-z])(?=.*[0-9])(?=(?:.*?[!##$%\^&*\(\)\-_+=;:'""\/\[\]{},.<>|`]){2}).{8,32}$
If you want to allow atleast 8 characters then you don't need to include 32 inside the range quantifier, just .{8,} would be enough.

Regular Expression Password Requirments and optional requirements

I am having a bit of a hard time with a password requirement regular expression for an ASP.NET project
Out requirements are the following
Must be at least 8 characters
Must have at least 3 of the 4 following:
Have at least 1 UPPERCASE letter
Have at least 1 lowercase letter
Have at least 1 special character
Have at least 1 number
The regular expression I am using is as follows (this is escaped and encoded for use in the web.config xml file:
passwordStrengthRegularExpression="^.*(?=.{8,})(?=.*[a-zA-Z])(?=.*\d)(?=.*[!##$%^&*()\?\+\,\-\.\/\:\:\;\<\=\>\[\]\\_\`\{\|\}\~\"\']).*$"
I cant figure out how to allow for one of the requirements to be optional.
the password Reaction7 should be sufficient, but it is rejected because it doesn't have a special character.
Anyone know what I can do to evaluate the 3 out of 4 requirements other than length?
Not sure I like this solution, but if you're limited to using only a single regex (which looks like the case), you could enumerate all possibilities with a pipe-or group:
passwordStrengthRegularExpression="^.*(?=.{8,})((?=.*[A-Z])(?=.*\d)(?=.*[!##$%^&*()\?\+\,\-\.\/\:\:\;\<\=\>\[\]\\_\`\{\|\}\~\"\'])|(?=.*[a-z])(?=.*\d)(?=.*[!##$%^&*()\?\+\,\-\.\/\:\:\;\<\=\>\[\]\\_\`\{\|\}\~\"\'])|(?=.*[a-z])(?=.*[A-Z])(?=.*[!##$%^&*()\?\+\,\-\.\/\:\:\;\<\=\>\[\]\\_\`\{\|\}\~\"\'])|(?=.*[a-z])(?=.*[A-Z])(?=.*\d)).*$"
It is rather long but does get the job done. Adding a fifth requirement will make this string explode in size though, so it's not exactly "extendable".

RegEx to Validate Password (1 Lowercase, 1Uppercase, 1 Digit, NoSpaces)

I'm trying to create a RegExpression to meet the criteria below;
at least 1 Lowercase
at least 1 Uppercase
at least 1 Digit
No Spaces
Minimum 8 characters
No special characters
So far I got this;
^(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.\s).*$
However I can not get it to work.
Any help would be greatly appreciated.
I was never good at puzzles :)
You're nearly there; it's just the .* at the end that ignores your "no spaces/special characters" rules, and the (?=.\s) lookahead is wrong (you probably meant (?!.*\s) or (?=\S*$)).
But you don't need that lookahead anyway because you can simply specify which characters are allowed (and enforce the "8 characters minimum" rule there, too):
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[A-Za-z\d]{8,}$
But why do you want to keep users from using non-alphanumeric characters in their passwords?

REGEX password validation without special characters

I am using this regex to validate my password.
My password -
should be alphanumeric ONLY,
contains at least 8 characters,
at least 2 numbers
and at least 2 alphabet.
My regex is
^.*(?=.{8,})(?=.*\d*\d)(?=.*[a-zA-Z]*[a-zA-Z])(?!.*\W).*$
but unfortunately it still matches if I try to put special characters at the beginning.
For example #password12, !password12.
Because your pattern begins and ends with .*, it will match anything at the beginning or end of the string, including special characters.
You shouldn't be solving this problem with a single regular expression, it makes the code hard to read and hard to modify. Write one function for each rule using whatever makes sense for that rule, then your validation script becomes crystal clear:
if is_alpha_only(password) &&
len(password) > = 8 &&
has_2_or_more_numbers(password) &&
has_2_or_more_alpha(password) ...
Seriously, what's the point of cramming all of that into a single regular expression?
And why disallow special characters? There's simply no reason for that.
You can use the following regex in case insensitive mode:
^(?=[a-z]*[0-9][a-z]*[0-9])^(?=[0-9]*[a-z][0-9]*[a-z])[a-z0-9]{8,}$
See it
I had a similar situation in which the client needed 4 alpha, 1 number, and between 8 and 20 characters. I've adapted my solution to your problem:
^(?=(?:[a-zA-Z0-9]*[a-zA-Z]){2})(?=(?:[a-zA-Z0-9]*\d){2})[a-zA-Z0-9]{8,}$
I understand the other answers dissuading you from this route, but sometimes the client wants what the client wants, regardless of your arguments to the contrary.