android的身份证校验——androidUtil方法

项目中用到了身份证校验,以前用的经验算法出现了问题,因此从androidUtil上直接找到了相关检验的方案

/**
* Regex of id card number which length is 15.
*/
public static final String REGEX_ID_CARD15 = “1\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3} " ; / ∗ ∗ ∗ R e g e x o f i d c a r d n u m b e r w h i c h l e n g t h i s 18. ∗ / p u b l i c s t a t i c f i n a l S t r i n g R E G E X I D C A R D 18 = " [ 1 − 9 ] d 5 [ 1 − 9 ] d 3 ( ( 0 d ) ∣ ( 1 [ 0 − 2 ] ) ) ( ( [ 0 ∣ 1 ∣ 2 ] d ) ∣ 3 [ 0 − 1 ] ) d 3 ( [ 0 − 9 X x ] ) "; /** * Regex of id card number which length is 18. */ public static final String REGEX_ID_CARD18 = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9Xx]) ";/Regexofidcardnumberwhichlengthis18./publicstaticfinalStringREGEXIDCARD18="[19]d5[19]d3((0d)(1[02]))(([012]d)3[01])d3([09Xx])”;

pu
blic static boolean isRightIdCard( final CharSequence input){
    if (input.length()>15){
      return   isMatch(REGEX_ID_CARD18,input);
    }else {
        return  isMatch(REGEX_ID_CARD15,input);
    }
}


/**
 * Return whether input matches the regex.
 *
 * @param regex The regex.
 * @param input The input.
 * @return {@code true}: yes<br>{@code false}: no
 */
public static boolean isMatch(final String regex, final CharSequence input) {
    return input != null && input.length() > 0 && Pattern.matches(regex, input);
}

/**
 * Return the list of input matches the regex.
 *
 * @param regex The regex.
 * @param input The input.
 * @return the list of input matches the regex
 */
public static List<String> getMatches(final String regex, final CharSequence input) {
    if (input == null) return Collections.emptyList();
    List<String> matches = new ArrayList<>();
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(input);
    while (matcher.find()) {
        matches.add(matcher.group());
    }
    return matches;
}

  1. 1-9 ↩︎

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值