package com.zhaoyang.driver.utils;
|
|
import java.util.regex.Matcher;
|
import java.util.regex.Pattern;
|
import java.util.regex.PatternSyntaxException;
|
|
/**
|
* Created by Administrator on 2017/5/12.
|
*/
|
|
public class PhoneCheckUtil {
|
/**
|
* 大陆号码或香港号码均可
|
*/
|
public static boolean isPhoneLegal(String str) throws PatternSyntaxException {
|
return isChinaPhoneLegal(str) || isHKPhoneLegal(str);
|
}
|
|
/**
|
* 大陆手机号码11位数,匹配格式:前三位固定格式+后8位任意数
|
* 此方法中前三位格式有:
|
* 13+任意数
|
* 15+除4的任意数
|
* 18+除1和4的任意数
|
* 17+除9的任意数
|
* 147
|
*/
|
public static boolean isChinaPhoneLegal(String str) throws PatternSyntaxException {
|
String regExp = "^((13[0-9])|(15[0-9])|(18[0-9])|(17[0-9])|(147))\\d{8}$";
|
Pattern p = Pattern.compile(regExp);
|
Matcher m = p.matcher(str);
|
return m.matches();
|
}
|
|
/**
|
* 香港手机号码8位数,5|6|8|9开头+7位任意数
|
*/
|
public static boolean isHKPhoneLegal(String str) throws PatternSyntaxException {
|
String regExp = "^(5|6|8|9)\\d{7}$";
|
Pattern p = Pattern.compile(regExp);
|
Matcher m = p.matcher(str);
|
return m.matches();
|
}
|
|
|
// 判断是否符合身份证号码的规范
|
public static boolean isLegalId(String IDCard) {
|
if (IDCard != null) {
|
String IDCardRegex = "(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x|Y|y)$)";
|
return IDCard.matches(IDCardRegex);
|
}
|
return false;
|
}
|
|
/**
|
* 校验手机号是否正确,校验规则(1开始共11位数字)
|
*
|
* @param phoneNumber
|
* @return
|
*/
|
public static boolean phoneCheck(String phoneNumber) {
|
boolean flag = false;
|
try {
|
String regExp = "^1\\d{10}$";
|
Pattern p = Pattern.compile(regExp);
|
Matcher m = p.matcher(phoneNumber);
|
flag = m.matches();
|
} catch (Exception e) {
|
}
|
return flag;
|
}
|
|
|
|
//二分查找方法
|
public static int binarySearch(long[] srcArray, long des) {
|
int low = 0;
|
int high = srcArray.length - 1;
|
while (low <= high) {
|
int middle = (low + high) / 2;
|
if (des == srcArray[middle]) {
|
return middle;
|
} else if (des < srcArray[middle]) {
|
high = middle - 1;
|
} else {
|
low = middle + 1;
|
}
|
}
|
return -1;
|
}
|
|
|
/**
|
* 当你输入信用卡号码的时候,有没有担心输错了而造成损失呢?其实可以不必这么担心,
|
* 因为并不是一个随便的信用卡号码都是合法的,它必须通过Luhn算法来验证通过。该校验的过程:
|
* 1、从卡号最后一位数字开始,逆向将奇数位(1、3、5等等)相加。
|
* 2、从卡号最后一位数字开始,逆向将偶数位数字,先乘以2(如果乘积为两位数,则将其减去9),再求和。
|
* 3、将奇数位总和加上偶数位总和,结果应该可以被10整除。例如,卡号是:5432123456788881
|
* 则奇数、偶数位(用红色标出)分布:5432123456788881奇数位和=35
|
* 偶数位乘以2(有些要减去9)的结果:16261577,求和=35。最后35+35=70可以被10整除,认定校验通过。
|
* 请编写一个程序,从键盘输入卡号,然后判断是否校验通过。通过显示:“成功”,否则显示“失败”。比如,用户输入:356827027232780
|
* 程序输出:成功
|
* <p>
|
* 判断是否是银行卡号
|
*
|
* @param cardNo
|
* @return
|
* @author WJ
|
*/
|
public static boolean checkBankCard(String cardNo) {
|
char bit = getBankCardCheckCode(cardNo
|
.substring(0, cardNo.length() - 1));
|
if (bit == 'N') {
|
return false;
|
}
|
return cardNo.charAt(cardNo.length() - 1) == bit;
|
|
}
|
|
private static char getBankCardCheckCode(String nonCheckCodeCardId) {
|
if (nonCheckCodeCardId == null
|
|| nonCheckCodeCardId.trim().length() == 0
|
|| !nonCheckCodeCardId.matches("\\d+")) {
|
// 如果传的不是数据返回N
|
return 'N';
|
}
|
char[] chs = nonCheckCodeCardId.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');
|
}
|
}
|