最近做一个项目,需要校验密码复杂性,想了想还是用正则表达式来判断方便快捷;
因密码是数字+字母组成,就只校验数字和字母是否存在;
例:
private String checkDataValid(String account, String pwd, String pwd_confirm) {
//正则表达式判断字母数字同时存在并不包含特殊字符
String reg = "^(?![a-zA-z]+$)(?!\\d+$)(?![!@#$%^&*]+$)[a-zA-Z\\d!@#$%^&*]+$";
if (TextUtils.isEmpty(account) | TextUtils.isEmpty(pwd) | TextUtils.isEmpty(pwd_confirm))
return getResources().getString(R.string.null_user_pwd);
if (pwd.length() < 8) {
return getResources().getString(R.string.pwd_len_not_less_eight);
}
if (!pwd.matches(reg)) {
return getResources().getString(R.string.pwd_must_num_letter);
}
if (!pwd.equals(pwd_confirm))
return getResources().getString(R.string.not_equal_hint);
if (account.length() != 11 && !account.contains("@"))
return getResources().getString(R.string.account_invalid_hint);
return "";
}
注:如有问题可以回复,看到第一时间分析解决,码农不易,感觉对您有用,帮助到您,可否打赏一杯可乐,在此谢过诸位,愿诸君终成大神,前程似锦~~~