.net - Regular expression for password with certain special characters excluding all others -


i have provide data annotation regex password that's specified as:

min 8 chars min 1 upper min 1 lower min 1 numeric min 1 special char can 1 of following:$|~=[]'_-+@. (and password can contain no other special chars besides these) 

it's exclusion of special characters giving me headache.

i've come not work:

"^.*(?=.{8,})(?=.*[a-z])(?=.*[a-z])(?=.*[\d])**(?(?!.*[^$|~=[\]'_\-+@.])|([^\w\w])).*$** 

it resolves enter invalid.

whereas (for special chars) on own work:

"(?(?!.*[^$|~=[\]'_\-+@.])|([^\w\w])).*$" 

and know first part works, missing make them work together?

alternatively, there simpler way of achieving this?

(.net environment)

if want in 1 regex pattern:

^(?=.*[a-z])(?=.*[a-z])(?=.*\d)(?=.*[$|~=[\]'_+@.-])[a-za-z0-9$|~=[\]'_+@.-]{8,}$ 

that should trick. require, lower case, upper case letter, digit , symbol lookaheads. note in character class, need move - end or escape it. otherwise creates character range don't want. use normal matching ensure there allowed characters, , @ least 8 of them.

however, it's better approach multiple tests. run these patterns individually:

[a-z]                     // input contain lower case letter? [a-z]                     // input contain upper case letter? \d                        // input contain digit? [$|~=[\]'_+@.-]           // input contain 1 of required symbols? [^a-za-z0-9$|~=[\]'_+@.-] // there invalid characters? 

where first 4 should return true , last 1 should return false. in addition can check input.length >= 8. makes more readable code , allow issue appropriate error message which condition not fulfilled.

in fact, since last pattern ensures there desired characters, can simplify "there has 1 symbol condition" [^a-za-z0-9] (in both approaches). i'm not sure whether makes things more or less readable.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -