java正则表达式的验证和使用实例(含SSH)

public void validate() {

// 匹配标识符必须由字母、数字、下划线组成,且开头和结尾不能有下划线,且中间的字符至少1个不能超过5个
String regex = "(^[a-z0-9A-Z])[a-z0-9A-Z_]{3,8}([a-z0-9-A-Z])";
// 验证邮箱格式
String Eregex = "^[\\w-]+(\\.[\\w-]+)*\\@([\\.\\w-]+)+$";
// 验证姓名
String Pregex = "^[\u4e00-\u9fa5]+$";
// 验证身份证号码
String Nregex="^(\\d{18,18}|\\d{15,15}|\\d{17,17}x)+$";
//验证手机号码
String phone="^(\\d{11})";
//验证地址
String address = "^[\u4e00-\u9fa5]+$";

if (userName == null || userName.replaceAll(" ", "").equals("")) {
addFieldError("userName", "输入的用户名不能为空");
}
if (password == null || password.replaceAll(" ", "").equals("")) {
addFieldError("password", "输入的密码不能为空");
}
//用正则表达式进行验证
else if(!Pattern.matches(regex, password) || !Pattern.matches(regex, password2)){
addFieldError(userName, "密码应该为5-10位数字、字符、下划线组成");
}else if(!password.equals(password2)){
addFieldError(password2, "两次密码不一致");
}
if (password2 == null || password2.replaceAll(" ", "").equals("")) {
addFieldError("password2", "输入的密码不能为空");
}
if (trueName == null || trueName.replaceAll(" ", "").equals("")|| ! Pattern.matches(Pregex, trueName)) {
addFieldError("trueName", "输入的真实名字格式不对");
}
if (personId == null || personId.replaceAll(" ", "").equals("")||!Pattern.matches(Nregex, personId)) {
addFieldError("personId", "输入的身份证号码格式不对");
}
if (email == null || email.replaceAll(" ", "").equals("")||!Pattern.matches(Eregex, email)) {
addFieldError("email", "输入的电子邮箱格式不对");
}
if (telphone == null || telphone.replaceAll(" ", "").equals("")||!Pattern.matches(phone, telphone)) {
addFieldError("telphone", "输入的电话号码格式不对");
}
if (address == null || address.replaceAll(" ", "").equals("")||!Pattern.matches(address, address)) {
addFieldError("address", "输入的通讯地址格式不对");
}


}

你可能感兴趣的:(java正则表达式的验证和使用实例(含SSH))