正则表达式验证RegExCheckUtil

package com.cs.common.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * @author 
 *
 */
public class RegExCheckUtil {
	/**
	* 身份证校验位
	*/
	public static String[] CHECK_DIGIT={"1","0","X","9","8","7","6","5","4","3","2"};
	/**
	* 身份证加权因子
	*/
	public static int[] gene={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1};
	
	
	/**
	* 判断是否为email
	* @param email
	* @return 是email返回true; 不是email返回false
	*/
	public static boolean isEmail(String email)
	{
	   if(email==null||email.equals(""))return false;
	   Pattern pattern = Pattern.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
	   Matcher isMail = pattern.matcher(email);
	   return isMail.matches();

	}
	/**
	* 判断字符串是否为数字
	* @param email
	* @return 是数字返回true; 不是数字返回false
	*/
	public static boolean isNum(String num)
	{
	   if(num==null ||num.equals(""))return false;
	   Pattern pattern = Pattern.compile("[0-9]*");
	   Matcher isNum = pattern.matcher(num);
	   return isNum.matches();
	}

	/**
	* 判断是否为手机号
	*/
	public static boolean isMobile(String mo)
	{
	   if(mo==null||mo.equals(""))return false;
	   Pattern pattern=Pattern.compile("^1[35]\\d{9}$");
	   Matcher isMobile=pattern.matcher(mo);
	   return isMobile.matches();
	}
	/**
	* 判断是否为身份证号^(\d{15}|(\d{17}[xX\d]))$
	*/
	public static boolean isIdentityCard(String card)
	{
	//   if(card==null||card.equals(""))return false;
	//   if(card.length()!=15&&card.length()!=18)return false;
	//   Pattern pattern=Pattern.compile("^(\\d{15}|(\\d{17}[xX\\d]))$");
	//   Matcher isIdentityCard=pattern.matcher(card);
	//   return isIdentityCard.matches();
	   if(card==null||card.equals(""))return false;
	   if(card.length()!=15&&card.length()!=18)return false;
	   Pattern pattern=Pattern.compile("^(\\d{15}|(\\d{17}[xX\\d]))$");
	   Matcher isIdentityCard=pattern.matcher(card);
	   if(!isIdentityCard.matches()) return false;
	   if(card.length()==18)
	   {
	    int yearPrefix=Integer.parseInt(card.substring(6,8));
	    if(yearPrefix<19||yearPrefix>21)return false;//出生日期必须大于1900年小于2100年
	    int month=Integer.parseInt(card.substring(10,12));
	    if(month>12||month==0)return false;      //验证月
	    int day=Integer.parseInt(card.substring(12,14));
	    if(day>31||day==0)return false;      //验证日
	    String checkDigit=getCheckDigitFor18(card);
	    if(checkDigit.equals("-1"))return false;
	    if(checkDigit.equals(card.substring(17,18).toUpperCase()))
	    {
	     return true;
	    }else
	    {
	     return false;
	    }
	   }else if(card.length()==15)
	   {
	    int month=Integer.parseInt(card.substring(8,10));
	    if(month>12||month==0)return false;      //验证月
	    int day=Integer.parseInt(card.substring(10,12));
	    if(day>31||day==0)return false; 
	    return true;
	   }
	   return false; 
	}

	private static String getCheckDigitFor18(String card)
	{
	   if(card==null||card.equals(""))return "-1";
	   int sum=0;
	   for(int i=0;i<17;i++)
	   {
	    sum+=Integer.parseInt(card.substring(i,i+1))*gene[i];
	   }
	   return CHECK_DIGIT[sum%11];
	}
	
	/**
	 * 判断电话号码是否为国内手机号码<br>
	 * 系统判断客户是国内手机用户(主叫号码前缀为13/15/18的11位有效数字)<br>
	 * 
	 * @param phoneNo
	 *            电话号码
	 * @return
	 */
	public static boolean isChinaPhoneNo(String phoneNo) {
		phoneNo = phoneNo.trim();
		if (phoneNo.length() < 11) {
			return false;
		} else {
			phoneNo = phoneNo.substring(phoneNo.length() - 11);

			String regEx = "^(13|15|18)\\d{9}$";
			Pattern pattern = Pattern.compile(regEx);
			Matcher matcher = pattern.matcher(phoneNo);
			return matcher.matches();
		}
	}

}

相关推荐