简体   繁体   中英

Regular Expression Match for Password

I'm building a form in ASP.Net using VB and I have a text box that I'd like to validate against the following rules:

  • Must be 6 - 20 characters long
  • Can Contain Letters and Could Be ALL letters
  • Can Contain The Following Special Characters: !@#$%^&*+-=(){}:;,'./?
  • Cannot contain any whitespace.
  • Cannot be all numbers and does not REQUIRE a number
  • The case does not matter

Those requirements aren't set by me...That's just what I have to work with.

So, the following would match:

TestUserPass
Te$tU$e^p@%}
testuserpass
test{user}n@ame

The following would not match:

8392039
dhj#5|3j

Hopefully that gives an idea of what I'm looking for...

Here's what I have so far: \\b[a-zA-Z0-9!@#\\$%\\^&\\*\\+=\\(\\){}:;,'\\./\\?-]{6,20}\\b

I believe that's working for everything EXCEPT the invalidating the instance where the entry is all numbers. It's the all numbers portion that I've been struggling with, though perhaps someone will find a flaw in what I already have. Any help is greatly appreciated!

You should use different anchors, and you can use a negative lookahead assertion to make sure that other characters than digits are present:

^(?![0-9]*$)[a-zA-Z0-9!@#\$%^&*+=(){}:;,'./?-]{6,20}$

Also, you don't need to escape (most) regex metacharacters within a character class as they don't have a special meaning there.

Use this regex

^(?!\d+$)[a-zA-Z\d!@#$%^&*+=(){}:;,'./?-]{6,20}$
 --------                                
     |
     |->dont match further if all are digits

- within character class represents a range and should be either escaped or should be at the beginning or at the end...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM