ID verification (JAVA, JS)

It was only when I was working on a project recently that I realized that the mantissa of the ID card is a check digit. To put it bluntly, it's not that you just input an ID card number is the correct number. Not only the province code of the first two digits must be correct, but the middle date of birth is a reasonable value, and the last digit of the ID card is a check code of the first 17 digits.

In fact, the logic is very simple, and you can understand it at a glance. Don’t talk nonsense, just go to the source code:

JAVA version

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

public class SimpleTest {

	static Map<String, Object> province;

	static {
		province = new HashMap<String, Object>();
		province.put("11", "北京");
		province.put("12", "天津");
		province.put("13", "河北");
		province.put("14", "山西");
		province.put("15", "内蒙古");
		province.put("21", "辽宁");
		province.put("22", "吉林");
		province.put("23", "黑龙江");
		province.put("31", "上海");
		province.put("32", "江苏");
		province.put("33", "浙江");
		province.put("34", "安徽");
		province.put("35", "福建");
		province.put("36", "江西");
		province.put("37", "山东");
		province.put("41", "河南");
		province.put("42", "湖北");
		province.put("43", "湖 南");
		province.put("44", "广东");
		province.put("45", "广西");
		province.put("46", "海南");
		province.put("50", "重庆");
		province.put("51", "四川");
		province.put("52", "贵州");
		province.put("53", "云南");
		province.put("54", "西藏");
		province.put("61", "陕西");
		province.put("62", "甘肃");
		province.put("63", "青海");
		province.put("64", "宁夏");
		province.put("65", "新疆");
		province.put("71", "台湾");
		province.put("81", "香港");
		province.put("82", "澳门");
		province.put("91", "国外");
	}

	public static void main(String[] args) {
		//你的身份证号码
		String content = "...";

		String pattern = "^\\d{17}(\\d|x|X)";
		if (!Pattern.matches(pattern, content)) {
			System.out.println("你输入的身份证长度或格式错误");
		}

		String provinceNum = content.substring(0, 2);
		if (!province.containsKey(provinceNum)) {
			System.out.println("你的身份证地区非法");
		}

		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Calendar ca = Calendar.getInstance();
		String year = content.substring(6, 10);
		String month = content.substring(10, 12);
		String date = content.substring(12, 14);
		ca.set(Integer.parseInt(year), Integer.parseInt(month)-1, Integer.parseInt(date));
		if (!sdf.format(ca.getTime()).equals(year + month + date)) {
			System.out.println("身份证上的出生日期非法");
		}
		int iSum=0;
		for (int i = 17; i >= 0; i--) {
			String ch = content.substring(17 - i, 18-i);
			if(ch.equals("X") || ch.equals("X")) {
				ch = "10";
			}
			iSum += (Math.pow(2, i) % 11) * Integer.parseInt(ch, 11);
		}
		if (iSum % 11 != 1) {
			System.out.println("你输入的身份证号非法");
		}
	}
}

JS version

    const checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('手机号不能为空'))
      } else {
        const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
        if (reg.test(value)) {
          callback()
        } else {
          return callback(new Error('请输入正确的手机号'))
        }
      }
    }
    const checkIdValidityPeriodList = (rule, value, callback) => {
      if (!value || value.length !== 2) {
        return callback(new Error('身份证有效期不能为空'))
      } else {
        callback()
      }
    }
    const checkIDCardNumber = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('身份证不能为空'))
      }
      let aCity = {
        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: '国外'
      }
      let iSum = 0
      if (!/^\d{17}(\d|x)$/i.test(value)) {
        return callback(new Error('你输入的身份证长度或格式错误'))
      }
      value = value.replace(/x$/i, 'a')
      if (aCity[parseInt(value.substr(0, 2))] == null) {
        return callback(new Error('你的身份证地区非法'))
      }
      let sBirthday = value.substr(6, 4) + '-' + Number(value.substr(10, 2)) + '-' + Number(value.substr(12, 2))
      let d = new Date(sBirthday.replace(/-/g, '/'))
      if (sBirthday !== (d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate())) {
        return callback(new Error('身份证上的出生日期非法'))
      }
      for (var i = 17; i >= 0; i--) {
        iSum += (Math.pow(2, i) % 11) * parseInt(value.charAt(17 - i), 11)
      }
      if (iSum % 11 !== 1) {
        return callback(new Error('你输入的身份证号非法'))
      }
      return callback()
    }

Guess you like

Origin blog.csdn.net/sadoshi/article/details/125654919