Android 개발-상용 인증 도구 클래스
public class VerificationUtil {
//
public static boolean isValidTelNumber(String telNumber) {
if (!TextUtils.isEmpty(telNumber)) {
String regex = "(\\+\\d+)?1[34578]\\d{9}$";
return Pattern.matches(regex, telNumber);
}
return false;
}
//
public static boolean isValidEmailAddress(String emailAddress) {
if (!TextUtils.isEmpty(emailAddress)) {
String regex = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
return Pattern.matches(regex, emailAddress);
}
return false;
}
// , ,
public static boolean isValidContent(String content) {
if (!TextUtils.isEmpty(content)) {
String regex = "^[\\w\\u4e00-\\u9fa5]+$";
return Pattern.matches(regex, content);
}
return false;
}
//
public static boolean isValidIdCard(String idCard) {
return IdcardValidator.isValidateIdcard(idCard);
}
//
public boolean isIdcard(String idCard) {
if (!TextUtils.isEmpty(idCard)) {
String regex = "(^\\d{15}$)|(\\d{17}(?:\\d|x|X)$)";
return Pattern.matches(regex, idCard);
}
return false;
}
// 15
public boolean is15Idcard(String idCard) {
if (!TextUtils.isEmpty(idCard)) {
String regex = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$";
return Pattern.matches(regex, idCard);
}
return false;
}
// 18
public boolean is18Idcard(String idCard) {
if (!TextUtils.isEmpty(idCard)) {
String regex = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([\\d|x|X]{1})$";
return Pattern.matches(regex, idCard);
}
return false;
}
/** * 〖 GB11643-1999〗 , , 。 * * : , , 。 * * : , 、 、 , , 。 * * ( ) : * 1. 17 。 :7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 * 2. 17 。 * 3. 11, ? * 4. 0 1 2 3 4 5 6 7 8 9 10 11 。 : 1 0 X 9 8 7 6 5 4 3 2; * 5. 2, 18 Ⅹ。 10, 2。 */
private static class IdcardValidator {
// ,
private static final String codeAndCity[][] = { { "11", " " }, { "12", " " },
{ "13", " " }, { "14", " " }, { "15", " " }, { "21", " " },
{ "22", " " }, { "23", " " }, { "31", " " }, { "32", " " },
{ "33", " " }, { "34", " " }, { "35", " " }, { "36", " " },
{ "37", " " }, { "41", " " }, { "42", " " }, { "43", " " },
{ "44", " " }, { "45", " " }, { "46", " " }, { "50", " " },
{ "51", " " }, { "52", " " }, { "53", " " }, { "54", " " },
{ "61", " " }, { "62", " " }, { "63", " " }, { "64", " " },
{ "65", " " }, { "71", " " }, { "81", " " }, { "82", " " },
{ "91", " " } };
//
private static final int power[] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
// 18
private static boolean isValidate18Idcard(String idcard) {
if (idcard.length() != 18) {
return false;
}
String idcard17 = idcard.substring(0, 17);
String idcard18Code = idcard.substring(17, 18);
char c[];
String checkCode;
if (isDigital(idcard17)) {
c = idcard17.toCharArray();
} else {
return false;
}
if (null != c) {
int bit[] = converCharToInt(c);
int sum17 = getPowerSum(bit);
// 11
checkCode = getCheckCodeBySum(sum17);
if (null == checkCode) {
return false;
}
// 18 ,
if (!idcard18Code.equalsIgnoreCase(checkCode)) {
return false;
}
}
return true;
}
// 15 18
public static String convertIdcarBy15bit(String idcard) {
String idcard18 = null;
if (idcard.length() != 15) {
return null;
}
if (isDigital(idcard)) {
//
String birthday = idcard.substring(6, 12);
Date birthdate = null;
try {
birthdate = new SimpleDateFormat("yyMMdd").parse(birthday);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cday = Calendar.getInstance();
cday.setTime(birthdate);
String year = String.valueOf(cday.get(Calendar.YEAR));
idcard18 = idcard.substring(0, 6) + year + idcard.substring(8);
char c[] = idcard18.toCharArray();
String checkCode = "";
if (null != c) {
int bit[] = converCharToInt(c);
int sum17;
sum17 = getPowerSum(bit);
// 11
checkCode = getCheckCodeBySum(sum17);
//
if (null == checkCode) {
return null;
}
// 17 18
idcard18 += checkCode;
}
} else {
return null;
}
return idcard18;
}
//
public static boolean isDigital(String str) {
return str == null || "".equals(str) ? false : str.matches("^[0-9]*$");
}
// ,
public static int getPowerSum(int[] bit) {
int sum = 0;
if (power.length != bit.length) {
return sum;
}
for (int i = 0; i < bit.length; i++) {
for (int j = 0; j < power.length; j++) {
if (i == j) {
sum = sum + bit[i] * power[j];
}
}
}
return sum;
}
// 11
private static String getCheckCodeBySum(int sum17) {
String checkCode = null;
switch (sum17 % 11) {
case 10:
checkCode = "2";
break;
case 9:
checkCode = "3";
break;
case 8:
checkCode = "4";
break;
case 7:
checkCode = "5";
break;
case 6:
checkCode = "6";
break;
case 5:
checkCode = "7";
break;
case 4:
checkCode = "8";
break;
case 3:
checkCode = "9";
break;
case 2:
checkCode = "x";
break;
case 1:
checkCode = "0";
break;
case 0:
checkCode = "1";
break;
}
return checkCode;
}
//
private static int[] converCharToInt(char[] c) throws NumberFormatException {
int[] a = new int[c.length];
int k = 0;
for (char temp : c) {
a[k++] = Integer.parseInt(String.valueOf(temp));
}
return a;
}
//
public static boolean isValidateIdcard(String idcard) {
if (!TextUtils.isEmpty(idcard)) {
if (idcard.length() == 15) {
return isValidate18Idcard(convertIdcarBy15bit(idcard));
} else if (idcard.length() == 18) {
return isValidate18Idcard(idcard);
}
}
return false;
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Opencv 주민등록번호 영역의 예시 코드 가 져 오기16 년 쯤 됐 을 때 한 공개 수업 에서 OCR 에 관 한 내용 을 봤 는데 OpenCV 를 통 해 주민등록번호 구역 을 재단 하고 Tess-Two 를 사용 하여 문자 인식 을 하여 주민등록번호 에 대한 식별 기능...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.