你的位置:首页 > ASP.net教程

[ASP.net教程]09.25常见正则表达式和用法


1.注册时验证注册密码的强度:

 public object IsValidPasswordStrength(string id, string value)    {      bool success = true;      string msg = string.Empty;      switch (SysGlobalInfo.PasswordStrength)      {        case PasswordStrength.Low:          Match m1 = new Regex(@"^(?:\d+|[a-zA-Z]+|[!@#$%^&*]+)$").Match(value);          if (!m1.Success)          {            success = false;            msg = "密码强度不够!密码组成规则 =(纯数字,纯字母,纯特殊字符)";          }          break;        case PasswordStrength.Medium:          Match m2 = new Regex(@"^(?![a-zA-z]+$)(?!\d+$)(?![!@#$%^&*]+$)[a-zA-Z\d!@#$%^&*]+$").Match(value);          if (!m2.Success)          {            success = false;            msg = "密码强度不够!密码组成规则 =(字母+数字,字母+特殊字符,数字+特殊字符)";          }          break;        case PasswordStrength.High:          Match m3 = new Regex(@"^(?![a-zA-z]+$)(?!\d+$)(?![!@#$%^&*]+$)(?![a-zA-z\d]+$)(?![a-zA-z!@#$%^&*]+$)(?![\d!@#$%^&*]+$)[a-zA-Z\d!@#$%^&*]+$").Match(value);          if (!m3.Success)          {            success = false;            msg = "密码强度不够!密码组成规则 =(字母+数字+特殊字符)";          }          break;      }      return new { valid = success, message = success ? string.Empty : msg };    }

验证密码强度