身份证合法性验证方法

写了个身份证合法性验证方法,留着以后Copy

 

 

	public static boolean isLegal(String idCardCode){
		if(idCardCode==null || (idCardCode=idCardCode.toUpperCase()).length()!=18
				&& idCardCode.length()!=15){
			return false;
		}
		if(idCardCode.length()==15){
			return Pattern.matches("^[1-6]\\d{5}\\d{2}[01]\\d[0-3]\\d{4}$", idCardCode);
		}else{
			boolean bs = Pattern.matches("^[1-6]\\d{5}(19|20)\\d{2}[01]\\d[0-3]\\d{4}[0-9X]$", idCardCode);
			if(bs){
				int sum = 0;
				int[] wi = new int[]{ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
				char[] ai = new char[]{'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
				for(int i=0; i<wi.length; ++i){
					int c = char2int(idCardCode.charAt(i));
					sum += wi[i] * c;
				}
				int mod = sum % 11;
				char c = idCardCode.charAt(17);
				bs = c==ai[mod];
			}
			return bs;
		}
	}

你可能感兴趣的:(身份证)