Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 952
StarFighter Avatar asked Feb 25 '26 02:02

StarFighter


1 Answers

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.

like image 176
Tim Pietzcker Avatar answered Feb 27 '26 19:02

Tim Pietzcker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!