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?
Related
I need to create a regular expression to check if a password has at least 1 uppercase letter, at least 2 numbers, and ends with a $ (dollar sign).
I've been trying to figure it out, but I can only get as far as checking if there's at least 1 uppercase and one number, rather than two.
These should be valid:
4hg5Fjkjk$
fh##Y5fFF5$
hgH5Hu6$
These should not be valid:
45tyghisu$ (No capital)
5THygfhy$ (Only one number)
Gh%hF45$h (No dollar sign at the end)
Here's what I have so far (checks for at least one number, one capital and dollar sign at the end)
/(?=.*[A-Z])(?=.*\d).*\$/
Any help would be greatly appreciated!
ps. I've looked on SO, and can't find anything relating to more than one required character.
In your pattern you have to repeat asserting a digit twice instead of one time using for example (?=(?:[^\d\r\n]*\d){2}) using contrast.
If you don't want to allow spaces in the password, you could use \S+ to match 1+ times a non whitespace char.
You could use:
^(?=[^A-Z\r\n]*[A-Z])(?=(?:[^\d\r\n]*\d){2})\S+\$$
Regex demo
According to the given answer by the OP, the number of characters should be 9-15:
^(?=[^A-Z\r\n]*[A-Z])(?=(?:[^\d\r\n]*\d){2})\S{9,15}\$$
Regex demo
This RegEx is simple and makes no other assumptions about what characters may be in a password other than what was specified by the OP.
^(?=.*?[A-Z])(?=.*?\d.*?\d).*\$$
See Demo (Click ON "RUN TESTS")
thanks for all the answers. Looked through them, and I figured out a pretty simple way to do it using the regular expression below. I edited it to allow for setting a length on the password, just change the 9 and 15 to your desired lengths.
/^(?=.*[A-Z])(?=.*\d.*\d)[^\s]{9,15}\$$/
I have a below regex in VBScript, Pattern:
^(?=.*[a-z])(?=.*[A-Z])(?!.*\s)(?=.*[0-9])(?=.*[!##\$&\*])(?=.{8,20}$)
This validates "length bet 8-20, one small, Capital, special char and digit each"
Issue#1
When I entered à , it passes the validation, which shouldn't have happened. How to restrict it ?
Issue#2
Later, I realized I can use keyboard of any language so I modified my regex to support all accented letters, but its not working either. Pattern:
^(?=.*\\p{L})(?!.*\s)(?=.*[0-9])(?=.*[!##\$&\*])(?=.{8,20}$)
Does VBScript allow to use p{L} regex ? any alternative ?
Your current pattern actually does not validate à. But it is slightly off and won't implement what you have in mind. Try this instead:
^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!##\$&*])[A-Za-z0-9!##$&*]{8,20}$
^^^ important
This says to assert that there is at least one:
lowercase letter
uppercase letter
digit
special character (!##$&*)
Then, it matches any of the above four types of characters 8 to 20 times.
The critical problem with your pattern, and the reason it would admit accented characters, provided the other assertions pass, is because of this:
(?=.{8,20})
Your final lookahead does enforce 8 to 20 characters, but it admits any character. Instead, I use a range limited only to the possible types of characters you want to appear.
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
I'm trying to build some regex that would detect when someone is trying to "fill out" their username with dots.
There are a few other requirements:
username must contain only letters, numbers and dots
username must start and end with a letter or number
but not more than one consecutive dot
minimum of 6 characters (letters and numbers)
e.g.:
a.b.c.d.e.6 is allowed (not caught) because it has 6 characters
a.b.c.d.5 is not (is caught) because it does not have the prerequisite 6 characters
The way that I'm building the regex is if there's a match, it will reject the username allowed.
What I have thus far is:
/[^a-z0-9.]|^\.|\.$|\.{2,}|\S{31,}|^\S{0,5}$/i
This catches:
any characters that aren't letters, numbers, dots
can't start with a dot
can't end with a dot
can't have 2 or more consecutive dots
can't have 31 or more characters
can't have 5 or less characters
I've tried dozens of different ways to get that last check in place, but they've all either broken the entire check, included the allowable (a.b.c.d.e.6) or just not worked.
the one that I've come closest with is:
(\.{1}[a-z0-9]{1,}){1,3}\S{1,}$
The problem with this is that it's also catching 123.456 (which should be allowed / not caught)
other examples of character strings that it should catch:
asdf.g
a.sdfg
a.sdf.g
as.df.g
I'm trying to do this using only regex, without having to pre-format it using JS.
Ok, after much experimentation I've actually found the answer. It turns out that finding the non-permitted strings was actually easier (for me anyway):
/^(\w\.?){4}\w$/
same expression expanded:
/^\w\.?\w\.?\w\.?\w\.?\w$/
This will catch anything that is populated with only 5 or fewer characters and interspersed with dots.
The full regex that I'm using also catches:
Strings of 31 or more characters (alphanumeric and periods).
Any characters that are not alphanumeric and periods.
Any string starting with a period
Any string ending with a period
Any string that has 2 or more consecutive periods
And a new-comer to the list: Any string that has 8 or more numeric digits without any alpha.
/^(\w\.?){4}\w$|^\w{0,5}$|\w{31,}|[^a-z0-9.]|^\.|\.$|\.{2,}|\d{8,}/i
I've tested this with all the possible combinations that I can think of on regex101 here: https://regex101.com/r/xI7wZ3/1
And it works! (yay)
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,}$