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;
        }
    }
}

좋은 웹페이지 즐겨찾기