c# – Password must have at least one non-alpha character

c# – Password must have at least one non-alpha character

Use regex pattern ^(?=.{8})(?=.*[^a-zA-Z])

n


n

Explanation:

n

^(?=.{8})(?=.*[^a-zA-Z])nxe2x94x82xe2x94x94xe2x94x80xe2x94x80xe2x94xacxe2x94x80xe2x94x80xe2x94x80xe2x94x98xe2x94x94xe2x94x80xe2x94x80xe2x94x80xe2x94x80xe2x94x80xe2x94x80xe2x94xacxe2x94x80xe2x94x80xe2x94x80xe2x94x80xe2x94x80xe2x94x80xe2x94x98nxe2x94x82   xe2x94x82           xe2x94x82nxe2x94x82   xe2x94x82           xe2x94x94 string contains some non-letter characternxe2x94x82   xe2x94x82nxe2x94x82   xe2x94x94 string contains at least 8 charactersnxe2x94x82nxe2x94x94 begining of line/stringn

n


n

If you want to limit also maximum length (lets say 16), then use regex pattern:

n

^(?=.{8,16}$)(?=.*[^a-zA-Z])n

Run it through a fairly simple regex: [^a-zA-Z]

n

And then check its length separately:

n

if(string.Length > 7)n

c# – Password must have at least one non-alpha character

A simple method will be like this:

n

Match match1 = Regex.Match(<input_string>, @(?=.{7}));   n

n

match1.Success ensures that there are at least 8 characters.

n

Match match2 = Regex.Match(<input_string>, [^a-zA-Z]);n

n

match2.Success ensures that there is at least one special character or number within the string.

n

So, match1.Success && match2.Success guarantees will get what you want.n

Leave a Reply

Your email address will not be published.