正则表达式验证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();
}
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值