js前端 c#后端密码验证策略-大小写特殊字符长度
//包含小写字母 private static readonly string REG_CONTAIN_LOWERCASE_ASSERTION = @"(?=.*[a-z])"; //包含大写字母 private static readonly string REG_CONTAIN_UPPERCASE_ASSERTION = @"(?=.*[A-Z])"; //包含数字 private static readonly string REG_CONTAIN_DIGIT_ASSERTION = @"(?=.*\d)"; //包含特殊字符 private static readonly string REG_CONTAIN_SPECIAL_CHAR_ASSERTION = @"(?=.*[ !""#$%&'()*+,-./:;<=>?@\[\]\^_`{|}~])"; public bool ValidatePwd(string pattern, string input) { Regex regex = new Regex(pattern); return regex.IsMatch(input); } public void Login(loginInfo obj) { if (obj.UserPwd.Length < 10) { // "密码至少有 10个字符长"; } else if ((ValidatePwd(REG_CONTAIN_LOWERCASE_ASSERTION, user.UserPwd) && ValidatePwd(REG_CONTAIN_UPPERCASE_ASSERTION, user.UserPwd) && ValidatePwd(REG_CONTAIN_DIGIT_ASSERTION, user.UserPwd) && ValidatePwd(REG_CONTAIN_SPECIAL_CHAR_ASSERTION, user.UserPwd)) || (!ValidatePwd(REG_CONTAIN_LOWERCASE_ASSERTION, user.UserPwd) && ValidatePwd(REG_CONTAIN_UPPERCASE_ASSERTION, user.UserPwd) && ValidatePwd(REG_CONTAIN_DIGIT_ASSERTION, user.UserPwd) && ValidatePwd(REG_CONTAIN_SPECIAL_CHAR_ASSERTION, user.UserPwd)) || (ValidatePwd(REG_CONTAIN_LOWERCASE_ASSERTION, user.UserPwd) && !ValidatePwd(REG_CONTAIN_UPPERCASE_ASSERTION, user.UserPwd) && ValidatePwd(REG_CONTAIN_DIGIT_ASSERTION, user.UserPwd) && ValidatePwd(REG_CONTAIN_SPECIAL_CHAR_ASSERTION, user.UserPwd)) || (ValidatePwd(REG_CONTAIN_LOWERCASE_ASSERTION, user.UserPwd) && ValidatePwd(REG_CONTAIN_UPPERCASE_ASSERTION, user.UserPwd) && !ValidatePwd(REG_CONTAIN_DIGIT_ASSERTION, user.UserPwd) && ValidatePwd(REG_CONTAIN_SPECIAL_CHAR_ASSERTION, user.UserPwd)) || (ValidatePwd(REG_CONTAIN_LOWERCASE_ASSERTION, user.UserPwd) && ValidatePwd(REG_CONTAIN_UPPERCASE_ASSERTION, user.UserPwd) && ValidatePwd(REG_CONTAIN_DIGIT_ASSERTION, user.UserPwd) && !ValidatePwd(REG_CONTAIN_SPECIAL_CHAR_ASSERTION, user.UserPwd))) { //通过 } else { //必须符合规则:至少有 10个字符长; 2. 至少包含以下四类字符中的三类字符:①英文大写字母(A 到 Z);②英文小写字 母(a 到 z);③10 个基本数字(0 到 9);④非字母字符(例如%、#、&); } }
js前端:
//是否包含数字 var ptr_digit = /^.*[0-9]+.*$/; //是否包含小写字母 var ptr_lowcase = /^.*[a-z]+.*$/; //是否包含大写字母 var ptr_upcase = /^.*[A-Z]+.*$/; //是否包含特殊字符(非数字、字母的字符) var ptr_special = /((?=[\x21-\x7e]+)[^A-Za-z0-9])/;
if (pwd.length < 10)
{ alert("密码至少有 10个字符长"); return false; } else if ((ptr_digit.test(pwd) && ptr_lowcase.test(pwd) && ptr_upcase.test(pwd) && ptr_special.test(pwd)) || (!ptr_digit.test(pwd) && ptr_lowcase.test(pwd) && ptr_upcase.test(pwd) && ptr_special.test(pwd)) || (ptr_digit.test(pwd) && !ptr_lowcase.test(pwd) && ptr_upcase.test(pwd) && ptr_special.test(pwd)) || (ptr_digit.test(pwd) && ptr_lowcase.test(pwd) && !ptr_upcase.test(pwd) && ptr_special.test(pwd)) || (ptr_digit.test(pwd) && ptr_lowcase.test(pwd) && ptr_upcase.test(pwd) && !ptr_special.test(pwd))) { //通过 } else
{ alert("必须符合规则:至少有 10个字符长; 2. 至少包含以下四类字符中的三类字符:①英文大写字母(A 到 Z);②英文小写字 母(a 到 z);③10 个基本数字(0 到 9);④非字母字符(例如%、#、&);"); return false; }