开发者

Password Regex (client side javascript)

开发者 https://www.devze.com 2023-03-11 04:29 出处:网络
I need a regex for the following criteria: Atleast 7 alphanumeric characters with 1 special character I used this:

I need a regex for the following criteria:

Atleast 7 alphanumeric characters with 1 special character

I used this:

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

It works fine if I type Password1! but doesnt work for PASSWORD1!.

Wont work for: Stmaryshsp1tal!

I am using the Jquery validation plugin where I specify the regex.

When I use a regu开发者_如何学Clar expression validator and specify the following regex:

^.*(?=.{7,})(?=(.*\W){1,}).*$

It works perfectly without any issues. When I set this regex in the Jquery validation I am using it doesnt work.

Please can someone shed some light on this? I want to understand why my first regex doesnt work.


(?=.\d)(?=.[a-z])

tries to match a digit and an alphanumeric character at the same place. Remember that (?= ... ) does not glob anything.

What you want is probably:

^(?=.*\W)(?=(.*\w){7})

This is exactly the same as veryfying that your string both matches ^.*\W (at least one special character) and ^(.*\w){7}) (7 alphanumeric characters. Note that it also matches if there are more.


Try this regex:

\S*[@#$!%^&+=]+\S*(?<=\S{7,})

EDIT3: Ok, this is last edit ;).

This will match also other special characters. So if you wan't limit the number of valid characters change \S to range of all valid characters.


Here is the regex , I think it can handle all possible combination..

^(?=.{7,})\w*[.@#$!%^&+=]+(\w*[.@#$!%^&+=]*)*$

here is the link for this regex, http://regexr.com?2tuh5


As a good tool for quickly testing regular expressions I'd suggest http://regexpal.com/ (no relations ;) ). Sometimes simplifying your expression helps a lot.

Then you might want to try something like ^[a-zA-Z0-9@#$!%^&+=]{7,}$

Update 2 now including digits

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

This matches:

  • Stmarysh3sptal!, password1!, PASSWORD1P!!!!!!@#^^ASSWORD1, 122ss121a212!!

... but not:

  • Password1, PASSWORD1PASSWORD1, PASSWORD!, Password!, 1221121212!! etc


The reason it matches Password1! but not PASSWORD1! is this clause:

(?=.*[a-z])

That requires at least one lowercase letter in the password. The pattern says that the password must be at least 7 characters long, and contain both uppercase and lowercase letters, at least one number, and at least one of @#$!%^&+=. PASSWORD1! fails because there are no lowercase letters in it.

The second pattern accepts PASSWORD1! because it's a far, far weaker password requirement. All it requires is that the password is 7+ characters and has at least one special character in it (other than _). The {1,} is unnecessary, by the way.

If I were you, I'd avoid weakening the password and just leave it as it is. If I wanted to allow all-lowercase or all-uppercase passwords for some reason, I'd simply change it to

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

...thus not weakening the password requirements any more than I had to.

0

精彩评论

暂无评论...
验证码 换一张
取 消