码迷,mamicode.com
首页 > 其他好文 > 详细

[正则表达式]常见字段的校验

时间:2014-12-29 11:36:18      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:

     正则表达式可以方便地对数据进行匹配,也可以执行更加复杂的字符串验证。

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

  1 import java.util.regex.Matcher;
  2 import java.util.regex.Pattern;
  3 
  4 public class Validate {
  5      //对生日的校验
  6      //格式:yyyy-mm-dd
  7      public static String isBirthday(String birthday){
  8          String errorMsg="";
  9          String pat="\\d{4}-\\d{1,2}-\\d{1,2}";
 10          Pattern p=Pattern.compile(pat);
 11          Matcher m=p.matcher(birthday);
 12          if(m.matches()){
 13              errorMsg="生日格式合法.";
 14          }else{
 15              errorMsg="生日格式不合法.";
 16          }
 17          return errorMsg;
 18      }
 19     
 20      //对账号的校验
 21      //格式:字母开头,允许6-12字节,允许字母数字下划线
 22      public static String isUsername(String username){
 23          String errorMsg="";
 24          String pat="[a-zA-Z][a-zA-Z0-9_]{5,11}";
 25          Pattern p=Pattern.compile(pat);
 26          Matcher m=p.matcher(username);
 27          if(m.matches()){
 28              errorMsg="账号格式合法.";
 29          }else{
 30              errorMsg="账号格式不合法.";
 31          }
 32          return errorMsg;
 33      }
 34 
 35      //对QQ号的校验
 36      //格式:从10000开始  5-11位
 37      public static String isQqnumber(String qqnumber){
 38          String errorMsg="";
 39          String pat="[1-9][0-9]{4,10}";
 40          Pattern p=Pattern.compile(pat);
 41          Matcher m=p.matcher(qqnumber);
 42          if(m.matches()){
 43              errorMsg="QQ号格式合法.";
 44          }else{
 45              errorMsg="QQ号格式不合法";         }
 46          return errorMsg;
 47      }
 48 
 49      //对手机号码的校验
 50      //格式:13 145 147 15 18开头
 51      public static String isPhonenumber(String phonenumber){
 52          String errorMsg="";
 53          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}";
 54          Pattern p=Pattern.compile(pat);
 55          Matcher m=p.matcher(phonenumber);
 56          if(m.matches()){
 57              errorMsg="手机号码格式合法.";
 58          }else{
 59              errorMsg="手机号码格式不合法.";
 60          }
 61          return errorMsg;
 62      }
 63 
 64      //对身份证号码的校验
 65      public static String isIdnumber(String idnumber){
 66          String errorMsg="";
 67          String pat="\\d{15}|\\d{18}"; 
 68          Pattern p=Pattern.compile(pat);
 69          Matcher m=p.matcher(idnumber);
 70          if(m.matches()){
 71              errorMsg="身份证号码格式合法.";
 72          }else{
 73              errorMsg="身份证号码格式不合法.";
 74          }
 75          return errorMsg;
 76      }
 77 
 78      //对电子邮箱的校验
 79      //格式:xxx@xxx.xxx
 80      public static String isAddress(String address){
 81          String errorMsg="";
 82          String pat="\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
 83          Pattern p=Pattern.compile(pat);
 84          Matcher m=p.matcher(address);
 85          if(m.matches()){
 86              errorMsg="电子邮箱格式合法.";
 87          }else{
 88              errorMsg="电子邮箱格式不合法.";
 89          }
 90          return errorMsg;
 91      }
 92 
 93      //对电话号码的校验
 94      //格式:XXX-XXXXXXXX或XXXX-XXXXXXX
 95      public static String isPhone(String phone){
 96          String errorMsg="";
 97          String pat="\\d{3}-\\d{8}|\\d{4}-\\d{7}";
 98          Pattern p=Pattern.compile(pat);
 99          Matcher m=p.matcher(phone);
100          if(m.matches()){
101              errorMsg="电话号码格式合法.";
102          }else{
103              errorMsg="电话号码格式不合法.";
104          }
105          return errorMsg;
106      }
107 
108      //对邮政编码的校验
109      //中国邮政编码为6位数字
110      public static String isPostnumber(String postnumber){
111          String errorMsg="";
112          String pat="[1-9]\\d{5}(?!\\d)";
113          Pattern p=Pattern.compile(pat);
114          Matcher m=p.matcher(postnumber);
115          if(m.matches()){
116              errorMsg="邮政编码格式合法.";
117          }else{
118              errorMsg="邮政编码格式不合法.";
119          }
120          return errorMsg;
121      }
122 
123      //对中文名的校验
124      //有两个以上中文
125      public static String isChinesename(String chinesename){
126          String errorMsg="";
127          String pat="[\u4e00-\u9fa5]{2,}";
128          Pattern p=Pattern.compile(pat);
129          Matcher m=p.matcher(chinesename);
130          if(m.matches()){
131              errorMsg="中文名格式合法.";
132          }else{
133              errorMsg="中文名格式不合法.";
134          }
135          return errorMsg;
136      }
137 
138      //对website的校验
139      public static String isWebsite(String website){
140          String errorMsg="";
141          String pat="http://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$";
142          Pattern p=Pattern.compile(pat);
143          Matcher m=p.matcher(website);
144          if(m.matches()){
145              errorMsg="website格式合法.";
146          }else{
147              errorMsg="website格式不合法.";
148          }
149          return errorMsg;
150      }
151 }

 

[正则表达式]常见字段的校验

标签:

原文地址:http://www.cnblogs.com/clannad493862452/p/4191109.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!