1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

South Africa ID number validation

Discussão em 'Angular' iniciado por thabang Nkosi, Outubro 9, 2024 às 23:32.

  1. I've researched this but none of the code I use seems to work. South African ID numbers contain the date of birth and gender. All I want is it to pull in that information and verify it when their ID number is entered into an input field, in angular

    I have tried using the javascript code that I currently have just modified it to be in typescript. Im not getting any errors but it's not validating at all.

    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);

    }

    Continue reading...

Compartilhe esta Página