Check if string contains at least one of each: lowercase letter, uppercase letter, number, and special character
asp.net, c#, regex
Solution
Change `(?=.*?^[a-zA-Z0-9_@.-])` with the below one:
+ see here
(?=.*?[^a-zA-Z0-9_@.-])
^^ i kept the dot, hyphen, etc as you used, if you don't need, remove.
In this regex, the `^` inside the character class `[]` is negating the characters. You were almost there, just unfortunately you have placed that outside the `[]`
Problem
I've searched SO and Google and most examples I find don't seem to work as intended (or don't combine all of these elements). I'm trying to create a Regex expression that matches (passes) if a string contains at least one of the following anywhere in the string and fails if it is missing any of them: - at least 8 characters in length - uppercase character - lowercase character - number - special character (including periods, underscores, etc., i.e. a whitelist approach is not preferable - allow any non-alphanumeric character) This is what I've tried: ``` if (System.Text.RegularExpressions.Regex.IsMatch(txtTest.Text.Trim(), "^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?^[a-zA-Z0-9_@.-]).{8,}$")) { lblMsg.Text = "Pass"; } else { lblMsg.Text = "Fail"; } ``` The problem is that this isn't working as intended. The following Pass when they should Fail (they don't have special characters): - 123cowboY - MonkeyCow123 It seems to work fine for detecting all but special characters. What did I do wrong and how do I fix it?