there is problem with you regular expression
Regex regex = new Regex(@"^(.{0,7}|[^0-9]*|[^A-Z])$");
you applied character |
which means either or.
form wiki
| -The choice (also known as alternation or set union) operator matches either the expression before or the expression after the operator. For example, abc|def matches "abc" or "def".
which means that in your regular expression it either matches .{0,7}
part or [^0-9]*|[^A-Z]
- that is why its returning true to you in any case.
You can use this regex:
^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$
This regex will enforce these rules:
• At least one upper case english letter
• At least one lower case english letter
• At least one digit
• At least one special character
• Minimum 8 in length
refered from : Regex for Password Must be contain at least 8 characters, least 1 number and both lower and uppercase letters and special characters