public class UserVerifyUtil {
/**
* 验证用户名,支持中英文(包括全角字符)、数字、下划线和减号 (全角及汉字算两位),长度为4-20位,中文按二位计数
*
* @param userName
* @return
* @author www.sangedabuliu.com
*/
public static boolean UserCountUtil(String userName) {
String validateStr = "^[\\w\\--_[0-9]\u4e00-\u9fa5\uFF21-\uFF3A\uFF41-\uFF5A]+$";
boolean rs = false;
rs = matcher(validateStr, userName);
if (rs) {
int strLenth = getStrLength(userName);
if (strLenth < 4 || strLenth > 20) {
rs = false;
}
}
return rs;
}
private static boolean matcher(String reg, String string) {
boolean tem = false;
Pattern pattern = Pattern.compile(reg);
Matcher matcher = pattern.matcher(string);
tem = matcher.matches();
return tem;
}
/**
* 获取字符串的长度,对双字符(包括汉字)按两位计数
*
* @param value
* @return
*/
public static int getStrLength(String value) {
int valueLength = 0;
String chinese = "[\u0391-\uFFE5]";
for (int i = 0; i < value.length(); i++) {
String temp = value.substring(i, i + 1);
if (temp.matches(chinese)) {
valueLength += 2;
} else {
valueLength += 1;
}
}
return valueLength;
}
/**
* 是否是手机号
*
* @param phone
* @return
*/
public static boolean isPhone(String phone) {
if (StringUtil.isBlank(phone)) {
return false;
}
String regex = "^1\\d{10}$";
if (phone.length() == 11) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(phone);
return m.matches();
}
return false;
}
}