一、判断字符串
//判断密码是否合格 必须大于8位,由大写字母,小写字母和数字组合
public static boolean isPasswordCorrect(String password) {
if (password.length() < 8) {
return false;
}
if (password.matches("\\w+")) {
Pattern p1 = Pattern.compile("[a-z]+");
Pattern p2 = Pattern.compile("[A-Z]+");
Pattern p3 = Pattern.compile("[0-9]+");
Matcher m = p1.matcher(password);
if (!m.find())
return false;
else {
m.reset().usePattern(p2);
if (!m.find())
return false;
else {
m.reset().usePattern(p3);
return m.find();
}
}
} else {
return false;
}
}
public static int isName(String name) {
int flag;
if (name.length() >= 2 && name.length() <= 10) {
if (name.matches("[A-Za-z0-9]{4,10}")) {
flag = AppConstants.LEGAL;
} else {//包含非法字符
flag = AppConstants.CONTENT_ILLEGAL;
}
} else {
//个数不合法 请输入2-10位的内容
flag = AppConstants.NUMBER_ILLEGAL;
}
return flag;
}
public static int isNameCorrect(String name) {
int flag;
if (name.length() >= 2 && name.length() <= 10) {
if (name.matches("[a-zA-Z0-9\\u4e00-\\u9fa5]{2,10}")) {
flag = AppConstants.LEGAL;
} else {
flag = AppConstants.CONTENT_ILLEGAL;
}
} else {
//个数不合法
flag = AppConstants.NUMBER_ILLEGAL;
}
return flag;
}
二、Double小数设置
//double 保留一位小数
public static double changeDouble(Double dou) {
NumberFormat nf = new DecimalFormat("0.0");
dou = Double.parseDouble(nf.format(dou));
return dou;
}
三、判断是否为手机号
public boolean isMobileNO(String mobiles) {
Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
Matcher m = p.matcher(mobiles);
return m.matches();
}