您的位置:首页 > 其它

[工具类-Utils]使用正则表达式对常见一些字段的校验

2014-12-29 10:08 513 查看
正则表达式可以方便地对数据进行匹配,也可以执行更加复杂的字符串验证。

下面是一些常见字段的校验,总结一下,以后总会用到的。

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Validate {
//对生日的校验
//格式:yyyy-mm-dd
public static String isBirthday(String birthday){
String errorMsg="";
String pat="\\d{4}-\\d{1,2}-\\d{1,2}";
Pattern p=Pattern.compile(pat);
Matcher m=p.matcher(birthday);
if(m.matches()){
errorMsg="生日格式合法.";
}else{
errorMsg="生日格式不合法.";
}
return errorMsg;
}

//对账号的校验
//格式:字母开头,允许6-12字节,允许字母数字下划线
public static String isUsername(String username){
String errorMsg="";
String pat="[a-zA-Z][a-zA-Z0-9_]{5,11}";
Pattern p=Pattern.compile(pat);
Matcher m=p.matcher(username);
if(m.matches()){
errorMsg="账号格式合法.";
}else{
errorMsg="账号格式不合法.";
}
return errorMsg;
}

//对QQ号的校验
//格式:从10000开始  5-11位
public static String isQqnumber(String qqnumber){
String errorMsg="";
String pat="[1-9][0-9]{4,10}";
Pattern p=Pattern.compile(pat);
Matcher m=p.matcher(qqnumber);
if(m.matches()){
errorMsg="QQ号格式合法.";
}else{
errorMsg="QQ号格式不合法";         }
return errorMsg;
}

//对手机号码的校验
//格式:13 145 147 15 18开头
public static String isPhonenumber(String phonenumber){
String errorMsg="";
String pat="(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}";
Pattern p=Pattern.compile(pat);
Matcher m=p.matcher(phonenumber);
if(m.matches()){
errorMsg="手机号码格式合法.";
}else{
errorMsg="手机号码格式不合法.";
}
return errorMsg;
}

//对身份证号码的校验
public static String isIdnumber(String idnumber){
String errorMsg="";
String pat="\\d{15}|\\d{18}";
Pattern p=Pattern.compile(pat);
Matcher m=p.matcher(idnumber);
if(m.matches()){
errorMsg="身份证号码格式合法.";
}else{
errorMsg="身份证号码格式不合法.";
}
return errorMsg;
}

//对电子邮箱的校验
//格式:xxx@xxx.xxx
public static String isAddress(String address){
String errorMsg="";
String pat="\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
Pattern p=Pattern.compile(pat);
Matcher m=p.matcher(address);
if(m.matches()){
errorMsg="电子邮箱格式合法.";
}else{
errorMsg="电子邮箱格式不合法.";
}
return errorMsg;
}

//对电话号码的校验
//格式:XXX-XXXXXXXX或XXXX-XXXXXXX
public static String isPhone(String phone){
String errorMsg="";
String pat="\\d{3}-\\d{8}|\\d{4}-\\d{7}";
Pattern p=Pattern.compile(pat);
Matcher m=p.matcher(phone);
if(m.matches()){
errorMsg="电话号码格式合法.";
}else{
errorMsg="电话号码格式不合法.";
}
return errorMsg;
}

//对邮政编码的校验
//中国邮政编码为6位数字
public static String isPostnumber(String postnumber){
String errorMsg="";
String pat="[1-9]\\d{5}(?!\\d)";
Pattern p=Pattern.compile(pat);
Matcher m=p.matcher(postnumber);
if(m.matches()){
errorMsg="邮政编码格式合法.";
}else{
errorMsg="邮政编码格式不合法.";
}
return errorMsg;
}

//对中文名的校验
//有两个以上中文
public static String isChinesename(String chinesename){
String errorMsg="";
String pat="[\u4e00-\u9fa5]{2,}";
Pattern p=Pattern.compile(pat);
Matcher m=p.matcher(chinesename);
if(m.matches()){
errorMsg="中文名格式合法.";
}else{
errorMsg="中文名格式不合法.";
}
return errorMsg;
}

//对website的校验
public static String isWebsite(String website){
String errorMsg="";
String pat="http://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$";
Pattern p=Pattern.compile(pat);
Matcher m=p.matcher(website);
if(m.matches()){
errorMsg="website格式合法.";
}else{
errorMsg="website格式不合法.";
}
return errorMsg;
}
151  }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: