南非身份证号码验证

时间:2019-10-23 12:20:14

标签: angular

我已经对此进行了研究,但是我使用的所有代码似乎都无效。南非身份证号包含出生日期和性别。我想要的是在输入他们的ID号并将其ID号输入到输入字段中时,输入该信息并进行验证

感谢您的帮助

我尝试使用目前刚刚将其修改为打字稿的javascript代码。我没有收到任何错误,但根本没有验证。

ts


 this.registerForm = this.formBuilder.group({
  confirm: ['', [Validators.required, Validators.email]],
};
      validateRSAidnumber(idnumber) {
        console.log('idnumber', idnumber);

        let invalid = 0;

        // check that value submitted is a number
        if (isNaN(idnumber)) {
          invalid++;
        }

        // check length of 13 digits
        if (idnumber.length !== 13) {
          invalid++;
        }

        // check that YYMMDD group is a valid date
        const yy = idnumber.substring(0, 2);
        const mm = idnumber.substring(2, 4);
        const dd = idnumber.substring(4, 6);

        const dob = new Date(yy, (mm - 1), dd);

        // check values - add one to month because Date() uses 0-11 for months
        if (!(((dob.getFullYear() + '').substring(2, 4) === yy) && (dob.getMonth() === mm - 1) && (dob.getDate() === dd))) {
          invalid++;
        }

        // evaluate GSSS group for gender and sequence 
        const gender = parseInt(idnumber.substring(6, 10), 10) > 5000 ? 'M' : 'F';

        // ensure third to last digit is a 1 or a 0
        if (idnumber.substring(10, 11) > 1) {
          invalid++;
        }

        // ensure second to last digit is a 8 or a 9
        if (idnumber.substring(11, 12) < 8) {
          invalid++;
        }

        // calculate check bit (Z) using the Luhn algorithm
        let ncheck = 0;
        let beven = false;

        for (let c = idnumber.length - 1; c >= 0; c--) {
          const cdigit = idnumber.charAt(c);
          let ndigit = parseInt(cdigit, 10);

          if (beven) {
            if ((ndigit *= 2) > 9) ndigit -= 9;
          }

          ncheck += ndigit;
          beven = !beven;
        }

        if ((ncheck % 10) !== 0) {
          invalid++;
        }

        return !invalid;
      }


      // convenience getter for easy access to form fields
      get f() { return this.registerForm.controls; }
      get isEmailMismatch() { return this.registerForm.getError('emailMismatch'); }

      onSubmit() {
        console.log('buttoj');
        this.submitted = true;

        this.userService.user.user.email = this.email;
        this.userService.user.user.first_name = this.firstName;
        this.userService.user.user.last_name = this.lastName;
        this.userService.user.user.id_number = this.idNumber;
        this.userService.user.user.password = this.password;
        this.userService.user.user.phone = '0' + this.contactNumber.toString();
        this.userService.user.user.id_number = this.idNumber.toString();
        this.registerUser();
        this.validateRSAidnumber(this.idNumber);

}

1 个答案:

答案 0 :(得分:0)

存储在南非身份证号码中的信息(例如 8801235111088):

  • 数字 1 和数字 2 = 出生年份(例如 1988)
  • 第 3 位和第 4 位 = 出生月份(例如 01/1 月)
  • 第 5 和第 6 位 = 出生日期(例如第 23 天)
  • 数字 7 到 10 用于定义性别(女性 = 0 到 4999;男性 = 5000 到 9999)(例如 5111 = 男性)
  • 数字 11 用于对您的公民身份进行分类(南澳公民 = 0;非南澳公民 = 1)
  • 直到 1980 年代才使用数字 12 对种族进行分类
  • 第 13 位最后一位用作校验和来确定 ID 号是否有效

这是从任何南非身份证号码中提取信息的 Python 脚本:

Id = str(input("Please enter your South African Identity Number(ID): "))
#Id = "8801235111088"

#Date of birth
year = int(Id[0:2])
month = Id[2:4]
day = Id[4:6]

if year >= 50:
    print("Date of birth is: ", day, month, "19" + str(year) )
if year < 50:
    print("Date of birth is: " , day, month, "20" + str(year))

#gender
gender = int(Id[6:10])
if 0000 <= gender <= 4999:
    print("Gender is : female")
if 5000 <= gender <= 9999:
    print("Gender is: male")


#Citizen status
citizen = int(Id[10])
if citizen == 0:
    print("You are a South African citizen")
if citizen == 1:
    print("You are not a South African citizen")