如何验证身份证号码是否有效?
望个位指点!

解决方案 »

  1.   

    我是用substring()来分段截取判断的,不知道还有不有更方便点的方法,高手指点下撒!
      

  2.   

       公民身份号码是特征组合码,由十七位数字本体码和一位校验码组成。排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码。 
    1、地址码(前六位数) 
    表示编码对象常住户口所在县(市、旗、区)的行政区划代码,按GB/T2260的规定执行。 
    2、出生日期码(第七位至十四位) 
    表示编码对象出生的年、月、日,按GB/T7408的规定执行,年、月、日代码之间不用分隔符。 
    3、顺序码(第十五位至十七位) 
    表示在同一地址码所标识的区域范围内,对同年、同月、同日出生的人编定的顺序号,顺序码的奇数分配给男性,偶数分配给女性。 
    4、校验码(第十八位数) 
    (1)十七位数字本体码加权求和公式 
    S = Sum(Ai * Wi), i = 0, ... , 16 ,先对前17位数字的权求和 
    Ai:表示第i位置上的身份证号码数字值 
    Wi:表示第i位置上的加权因子 
    Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 
    (2)计算模 
    Y = mod(S, 11) 
    (3)通过模得到对应的校验码 
    Y: 0 1 2 3 4 5 6 7 8 9 10 
    校验码: 1 0 X 9 8 7 6 5 4 3 2
      

  3.   

    import java.io.*;public class ID{
     private String temp ;
     private final char[] cc = {1,0,'X',9,8,7,6,5,4,3,2};
     public String getID(){
      try{
       System.out.println("Input ID,please [input]");
       BufferedReader br = new BufferedReader(new InputStreamReader(
        System.in));
        temp = br.readLine();
      }
      catch(IOException e){}
      System.out.println(" make sure of it:"+temp);
      return temp;
     }
     
     public void checkit(){
      String temp1 = temp;
      if(temp1.length() == 18){
       System.out.println(temp1.substring(6,10)+"year "+
        temp1.substring(10,12)+"month "+
         temp1.substring(12,14)+"day  "+
          "sex: "+checkSex(temp1.charAt(16)) );
      }
      else if(temp1.length() == 15){
       String id = makeit(temp1);
       System.out.println(id.substring(6,10)+"year "+
        id.substring(10,12)+"month "+
         id.subSequence(12,14)+"day  "+
          "sex: "+ checkSex(id.charAt(16)));
      }
      else
       System.out.println("your input is Error,please input again!");
     }
     
     public String checkSex(char c){
      if(c%2 == 0)
       return "girl";
      else
       return "boy";
     }
     
     public int LastNum(String temp3){
      int index = 0;
      for(int i = 16;i>= 0;i--){
       double wi = Math.pow(2,(i-1)) %11;
       int ai = (int)temp3.charAt(i);
       index += ai*wi;
      }
      index = index % 11;
      
      index = (int)this.cc[index];
      return index;
     }
     
     public String makeit(String temp2){
      StringBuffer temp3 = new StringBuffer(temp2);
      temp3.insert(6,"19");
      temp3.insert(17,LastNum(temp3.toString()));
      return temp3.toString();
     }
     
     public static void main(String[] args){
      ID id = new ID();
      id.temp = id.getID();
      id.checkit();
     }
     
    }