验证
package com.dzqc.dz.main.util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 验证
* @author Administrator
*
*/
public class J {
private static final byte MALE = 1;
private static final byte FAMALE = 0;
/**
* 检验身份证是否合法
* @return 1-合法;0-不合法
*/
public static int isLegal(String idcard){
if(idcard.length()<18) {
return 0;
}
int a = 0;
int sum = 0;
char checkBit[]={'1','0','X','9','8','7','6','5','4','3','2'};
int []add={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
char []stringArr = idcard.toCharArray();
for (int i=0;i<17;i++){
sum +=add[i]*(stringArr[i]-'0');
}
if (stringArr[17]==checkBit[sum%11]){
a=1;
}
return a;
}
/**
* 判断性别
* @return 1-男;0-女;-1-错误;
*/
public static byte sex(String idcard){
if (isLegal(idcard)==1){
char []stringArr = idcard.toCharArray();
if (stringArr[17]%2==0){
return MALE;
}else{
return FAMALE;
}
}
return -1;
}
/**
* 输出年月日
* @return 年月日的字符串比如:20171207
*/
public static String year(String idcard){
String num=null;
if (isLegal(idcard)==1){
num = idcard.substring(6,14);
}
return num;
}
/**
* 判断字符串是否为空
* @param t
* @return
*/
public static boolean isNull(String t) {
if(t==null||"".equals(t)) {
return true;
}else {
return false;
}
}
/**
* 判断是否为纯数字
* @param t
* @return
*/
public static boolean isNum(String t) {
char ch;
t=t.trim();
for(int i=0,j=t.length();i<j;i++) {
ch=t.charAt(i);
if(!((ch>='0' && ch<='9'))) {
return false;
}
}
return true;
}
/**
* 判断是否含有数字
* @param t
* @return
*/
public static boolean haveNum(String t) {
char ch;
t=t.trim();
for(int i=0,j=t.length();i<j;i++) {
ch=t.charAt(i);
if(((ch>='0' && ch<='9'))) {
return true;
}
}
return false;
}
/**
* 验证手机号
* @param phone
* @return
*/
public static boolean isPhone(String phone) {
String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
if (phone.length() != 11) {
return false;
} else {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(phone);
boolean isMatch = m.matches();
return isMatch;
}
}
}
银行卡验证:
/**
* 校验银行卡卡号
*/
public static boolean checkBankCard(String bankCard) {
if(bankCard.length() < 15 || bankCard.length() > 19) {
return false;
}
char bit = getBankCardCheckCode(bankCard.substring(0, bankCard.length() - 1));
if(bit == 'N'){
return false;
}
return bankCard.charAt(bankCard.length() - 1) == bit;
}
/**
* 从不含校验位的银行卡卡号采用 Luhm 校验算法获得校验位
* @param nonCheckCodeBankCard
* @return
*/
private static char getBankCardCheckCode(String nonCheckCodeBankCard){
if(nonCheckCodeBankCard == null || nonCheckCodeBankCard.trim().length() == 0
|| !nonCheckCodeBankCard.matches("\\d+")) {
//如果传的不是数据返回N
return 'N';
}
char[] chs = nonCheckCodeBankCard.trim().toCharArray();
int luhmSum = 0;
for(int i = chs.length - 1, j = 0; i >= 0; i--, j++) {
int k = chs[i] - '0';
if(j % 2 == 0) {
k *= 2;
k = k / 10 + k % 10;
}
luhmSum += k;
}
return (luhmSum % 10 == 0) ? '0' : (char)((10 - luhmSum % 10) + '0');
}