Checking password input with regular expression

J

JJ

To validate a password as the user is registering I want to use a regular
expressio validator.

I got this one from the Microsoft web site for validating a password of at
least 7 characters, with at least one number and one non-alphanumeric
character:

@\"(?=.{7,})(?=(.*\d){1,})(?=(.*\W){1,})

Yet I can't get it to validate that a password like qwertyui123# fits the
criteria.

Or any password for that matter. Can anyone help with a regular expression
that works? or am I goong wrong somewhere?

JJ
 
M

Markus Palme

I got this one from the Microsoft web site for validating a password of at
least 7 characters, with at least one number and one non-alphanumeric
character:

@\"(?=.{7,})(?=(.*\d){1,})(?=(.*\W){1,})

Yet I can't get it to validate that a password like qwertyui123# fits the
criteria.

@\"(?=.{7,})(?=(.*\d){1,})(?=(.*\w){1,})

This one works, just a typing mistake.

Markus
 
J

JJ

Markus,

Surely by changing the 'W' to 'w' you are not looking for patterns with
non-alphanumeric characters in it?

the (?=*\W){1}) means the pattern must have one or more non-alphanumeric
characters in it.

I think the problem lies with the initial @\" . Clearly the expression is
trying to match a pattern that does not have a '@' or a ' " ' in it, but if
you remove this part of the expression, it works as expected, allbeit
allowing those 2 'disallowed' characters.

Anyone work out what the correct notation should be?

JJ
 
M

Markus Palme

JJ said:
Markus,

Surely by changing the 'W' to 'w' you are not looking for patterns with
non-alphanumeric characters in it?

My mistake, sorry - I missed the "non-"alphanumeric. The regex of
course, is only this part:

(?=.{7,})(?=(.*\d){1,})(?=(.*\w){1,})

Markus
 
J

JJ

Sorry Markus I still can't see that it makes sense to me.

If I am searching for a pattern that matches 7 or more characters containing
1 or more digits and 1 or more non-alphanumeric characters,
I can't see how this would work.

I understand I may be going wrong somewhere, but where?

The original @\" at the beginning was meant to make sure that those
characters are not part of the password. So, as far as I can see they do
need to be part of the regular expression.

JJ
 
J

JJ

How about this one:

^.*(?=.{7,})(?=.*\d)(?=.*[a-zA-Z])(?=.*[@#$%^&+=]).*$

That breaks down as:

7 characters;
must contain a digit;
must contain a character in the range a-z or A-Z;
must contain one of these characters @#$%^&+=

I've run some quick tests and its seems to work for me, but be grateful for
anyone elses views?

JJ
 
T

ThunderMusic

ok, so Here it is, the supreme regex for passwords... ;)

^.*(?=.{7,})((?=.*\d)(?=.*[a-z])(?=.*[A-Z])|(?=.*\d)(?=.*[a-z])(?=.*[^a-zA-Z0-9])|(?=.*\d)(?=.*[A-Z])(?=.*[^a-zA-Z0-9])|(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])).*$

it tests for 3 of these 4 cases : 1 a-z, 1 A-Z, 1 digit, 1 non-alphanumeric.
7 chars minimum... you can test it, it works fine... I'll add it to my
validations.

I hope helps

ThunderMusic


JJ said:
How about this one:

^.*(?=.{7,})(?=.*\d)(?=.*[a-zA-Z])(?=.*[@#$%^&+=]).*$

That breaks down as:

7 characters;
must contain a digit;
must contain a character in the range a-z or A-Z;
must contain one of these characters @#$%^&+=

I've run some quick tests and its seems to work for me, but be grateful
for anyone elses views?

JJ




JJ said:
Sorry Markus I still can't see that it makes sense to me.

If I am searching for a pattern that matches 7 or more characters
containing 1 or more digits and 1 or more non-alphanumeric characters,
I can't see how this would work.

I understand I may be going wrong somewhere, but where?

The original @\" at the beginning was meant to make sure that those
characters are not part of the password. So, as far as I can see they do
need to be part of the regular expression.

JJ
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top