<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    tinguo002

     

    JAVA身份證驗證

    public class IDCard {

          private String _codeError;

          //wi =2(n-1)(mod 11)
          final int[] wi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1};
          // verify digit
          final int[] vi = {1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2};
          private int[] ai = new int[18];
          private static String[] _areaCode={"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 HashMap<String,Integer> dateMap;
          private static HashMap<String,String> areaCodeMap;
          static{
                dateMap=new HashMap<String,Integer>();
                dateMap.put("01",31);
                dateMap.put("02",null);
                dateMap.put("03",31);
                dateMap.put("04",30);
                dateMap.put("05",31);
                dateMap.put("06",30);
                dateMap.put("07",31);
                dateMap.put("08",31);
                dateMap.put("09",30);
                dateMap.put("10",31);
                dateMap.put("11",30);
                dateMap.put("12",31);
                areaCodeMap=new HashMap<String,String>();
                for(String code:_areaCode){
                      areaCodeMap.put(code,null);
                }
          }

          //驗證身份證位數,15位和18位身份證
          public boolean verifyLength(String code){
                int length=code.length();
                if(length==15 || length==18){
                      return true;
                }else{
                      _codeError="錯誤:輸入的身份證號不是15位和18位的";
                      return false;
                }
          }

          //判斷地區(qū)碼
          public boolean verifyAreaCode(String code){
                String areaCode=code.substring(0,2);
    //            Element child=  _areaCodeElement.getChild("_"+areaCode);
                if(areaCodeMap.containsKey(areaCode)){
                      return true;
                }else{
                      _codeError="錯誤:輸入的身份證號的地區(qū)碼(1-2位)["+areaCode+"]不符合中國行政區(qū)劃分代碼規(guī)定(GB/T2260-1999)";
                      return false;
                }
          }

          //判斷月份和日期
          public boolean verifyBirthdayCode(String code){
                //驗證月份
                String month=code.substring(10,12);
                boolean isEighteenCode=(18==code.length());
                if(!dateMap.containsKey(month)){
                      _codeError="錯誤:輸入的身份證號"+(isEighteenCode?"(11-12位)":"(9-10位)")+"不存在["+month+"]月份,不符合要求(GB/T7408)";
                      return false;
                }
                //驗證日期
                String dayCode=code.substring(12,14);
                Integer day=dateMap.get(month);
                String yearCode=code.substring(6,10);
                Integer year=Integer.valueOf(yearCode);

                //非2月的情況
                if(day!=null){
                      if(Integer.valueOf(dayCode)>day || Integer.valueOf(dayCode)<1){
                            _codeError="錯誤:輸入的身份證號"+(isEighteenCode?"(13-14位)":"(11-13位)")+"["+dayCode+"]號不符合小月1-30天大月1-31天的規(guī)定(GB/T7408)";
                            return false;
                      }
                }
                //2月的情況
                else{
                      //閏月的情況
                      if((year%4==0&&year%100!=0)||(year%400==0)){
                            if(Integer.valueOf(dayCode)>29 || Integer.valueOf(dayCode)<1){
                                  _codeError="錯誤:輸入的身份證號"+(isEighteenCode?"(13-14位)":"(11-13位)")+"["+dayCode+"]號在"+year+"閏年的情況下未符合1-29號的規(guī)定(GB/T7408)";
                                  return false;
                            }
                      }
                      //非閏月的情況
                      else{
                            if (Integer.valueOf(dayCode) > 28 || Integer.valueOf(dayCode) < 1) {
                                  _codeError="錯誤:輸入的身份證號"+(isEighteenCode?"(13-14位)":"(11-13位)")+"["+dayCode+"]號在"+year+"平年的情況下未符合1-28號的規(guī)定(GB/T7408)";
                                  return false;
                            }
                      }
                }
                return true;
          }

          //驗證身份除了最后位其他的是否包含字母
          public boolean containsAllNumber(String code) {
                String str="";
                if(code.length()==15){
                      str=code.substring(0,15);
                }else if(code.length()==18){
                      str=code.substring(0,17);
                }
                char[] ch = str.toCharArray();
                for (int i = 0; i < ch.length; i++) {
                      if (! (ch[i] >= '0' && ch[i] <= '9')) {
                            _codeError="錯誤:輸入的身份證號第"+(i+1)+"位包含字母";
                            return false;
                      }
                }
                return true;
          }

          public String getCodeError(){
                return _codeError;
          }

          //驗證身份證
          public boolean verify(String idcard) {
                _codeError="";
                //驗證身份證位數,15位和18位身份證
                if(!verifyLength(idcard)){
                    return false;
                }
                //驗證身份除了最后位其他的是否包含字母
                if(!containsAllNumber(idcard)){
                      return false;
                }

                //如果是15位的就轉成18位的身份證
                String eifhteencard="";
                if (idcard.length() == 15) {
                      eifhteencard = uptoeighteen(idcard);
                }else{
                      eifhteencard=idcard;
                }
                //驗證身份證的地區(qū)碼
                if(!verifyAreaCode(eifhteencard)){
                      return false;
                }
                //判斷月份和日期
                if(!verifyBirthdayCode(eifhteencard)){
                      return false;
                }
                //驗證18位校驗碼,校驗碼采用ISO 7064:1983,MOD 11-2 校驗碼系統(tǒng)
                if(!verifyMOD(eifhteencard)){
                      return false;
                }
                return true;
          }

          //驗證18位校驗碼,校驗碼采用ISO 7064:1983,MOD 11-2 校驗碼系統(tǒng)
          public boolean verifyMOD(String code){
                String verify = code.substring(17, 18);
                if("x".equals(verify)){
                      code=code.replaceAll("x","X");
                      verify="X";
                }
                String verifyIndex=getVerify(code);
                if (verify.equals(verifyIndex)) {
                      return true;
                }
    //            int x=17;
    //            if(code.length()==15){
    //                  x=14;
    //            }
                _codeError="錯誤:輸入的身份證號最末尾的數字驗證碼錯誤";
                return false;
          }

          //獲得校驗位
          public String getVerify(String eightcardid) {
                int remaining = 0;

                if (eightcardid.length() == 18) {
                      eightcardid = eightcardid.substring(0, 17);
                }

                if (eightcardid.length() == 17) {
                      int sum = 0;
                      for (int i = 0; i < 17; i++) {
                            String k = eightcardid.substring(i, i + 1);
                            ai[i] = Integer.parseInt(k);
                      }

                      for (int i = 0; i < 17; i++) {
                            sum = sum + wi[i] * ai[i];
                      }
                      remaining = sum % 11;
                }

                return remaining == 2 ? "X" : String.valueOf(vi[remaining]);
          }

          //15位轉18位身份證
          public String uptoeighteen(String fifteencardid) {
                String eightcardid = fifteencardid.substring(0, 6);
                eightcardid = eightcardid + "19";
                eightcardid = eightcardid + fifteencardid.substring(6, 15);
                eightcardid = eightcardid + getVerify(eightcardid);
                return eightcardid;
          }



    調 用 new IDCard().verify(身份證id);
    轉載:http://www.oschina.net/code/snippet_249203_24013


    歡迎大家訪問我的個人網站 萌萌的IT人

    posted on 2014-02-27 10:45 一堣而安 閱讀(480) 評論(0)  編輯  收藏 所屬分類: java

    導航

    統(tǒng)計

    常用鏈接

    留言簿(1)

    隨筆分類

    隨筆檔案

    收藏夾

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 在线免费观看亚洲| 亚洲AV无码国产精品麻豆天美 | 亚洲hairy多毛pics大全| 99精品国产成人a∨免费看| 亚洲乱亚洲乱妇无码麻豆| 在线视频网址免费播放| 亚洲狠狠婷婷综合久久久久| a级毛片免费网站| 亚洲性猛交XXXX| 182tv免费视频在线观看| 亚洲成A人片777777| 99xxoo视频在线永久免费观看| 亚洲国产精品国自产电影| 18禁美女裸体免费网站| 亚洲情A成黄在线观看动漫软件| 最近中文字幕mv免费高清电影| 亚洲精品宾馆在线精品酒店| 免费观看亚洲人成网站| 一级特黄特色的免费大片视频| 在线亚洲精品福利网址导航| 免费观看在线禁片| 亚洲国产成人久久精品app| 黑人粗长大战亚洲女2021国产精品成人免费视频 | 美女啪啪网站又黄又免费| 国产成人高清亚洲| 小草在线看片免费人成视久网| 亚洲精品美女在线观看| 国产精品无码免费播放| 一本久久A久久免费精品不卡| 亚洲乱码一区二区三区在线观看| 桃子视频在线观看高清免费视频 | 看亚洲a级一级毛片| 国产亚洲情侣一区二区无| 国产成人一区二区三区视频免费| 亚洲国产日韩女人aaaaaa毛片在线| 性色av免费观看| 一级特黄特色的免费大片视频| 91在线精品亚洲一区二区| 日本一区免费电影| 久久久久久久99精品免费| 亚洲乱亚洲乱妇24p|