代码改变世界

验证密码强度是否符合要求

2017-01-06 21:12  sunice  阅读(1016)  评论(0编辑  收藏  举报

密码强度要求:

1、密码长度至少为8个字符

2、至少要有一个大写英文或者小写字母

3、最少包含一个数字

备注:该方法也可以拆成4个if判断,可以针对不同的情况,对注册客户进行提示。

 1 public static bool PasswordStrength(string password)
 2 {
 3     if (password.Length <8)
 4     {
 5         return false;
 6     }
 7     if (0 - Convert.ToInt32(Regex.IsMatch(password, "[a-z]")) 
 8           - Convert.ToInt32(Regex.IsMatch(password, "[A-Z]")) 
 9           - Convert.ToInt32(Regex.IsMatch(password, "\\d")) 
10           - Convert.ToInt32(Regex.IsMatch(password, ".{10,}")) <= -2)
11     {
12         return true;
13     }
14     else
15     {
16         return false;
17     }
18 }