south african id number validation code example

Example 1: south african id number validation c#

//Validate ID Number 
//JQuery
<script>


function isNumber(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
}

    $('#idCheck').click(function () {

 // first clear any left over error messages
    $('#error p').remove();
    // store the error div, to save typing
    var error = $('#error');

    var idNumber = $('#check').val();


    // assume everything is correct and if it later turns out not to be, just set this to false
    var correct = true;

    //Ref: http://www.sadev.co.za/content/what-south-african-id-number-made
    // SA ID Number have to be 13 digits, so check the length
        if (idNumber.length != 13 || !isNumber(idNumber))
        {
        error.append('<p>ID number does not appear to be authentic</p>');
        correct = false;
         }

    // get first 6 digits as a valid date
    var tempDate = new Date(idNumber.substring(0, 2), idNumber.substring(2, 4) - 1, idNumber.substring(4, 6));

    var id_date = tempDate.getDate();
    var id_month = tempDate.getMonth();
    var id_year = tempDate.getFullYear();

    var fullDate = id_date + "-" + id_month + 1 + "-" + id_year;

    if (!((tempDate.getYear() == idNumber.substring(0, 2)) && (id_month == idNumber.substring(2, 4) - 1) && (id_date == idNumber.substring(4, 6)))) {
        //error.append('<p>ID number does not appear to be authentic - date part not valid</p>');
        correct = false;
    }

    // get the gender
    var genderCode = idNumber.substring(6, 10);
    var gender = parseInt(genderCode) < 5000 ? "Female" : "Male";

    // get country ID for citzenship
    var citzenship = parseInt(idNumber.substring(10, 11)) == 0 ? "Yes" : "No";

    // apply Luhn formula for check-digits
    var tempTotal = 0;
    var checkSum = 0;
    var multiplier = 1;
    for (var i = 0; i < 13; ++i) {
        tempTotal = parseInt(idNumber.charAt(i)) * multiplier;
        if (tempTotal > 9) {
            tempTotal = parseInt(tempTotal.toString().charAt(0)) + parseInt(tempTotal.toString().charAt(1));
        }
        checkSum = checkSum + tempTotal;
        multiplier = (multiplier % 2 == 0) ? 1 : 2;
    }
    if ((checkSum % 10) != 0) {
        //error.append('<p>ID number does not appear to be authentic - check digit is not valid</p>');
        correct = false;
    };


    // if no error found, hide the error message
    if (correct) {
        error.css('display', 'none');

        // clear the result div
        $('#result').empty();
        // and put together a result message
        $('#result').append('<p>South African ID Number:   ' + idNumber + '</p><p>Birth Date:   ' + fullDate + '</p><p>Gender:  ' + gender + '</p><p>SA Citizen:  ' + citzenship + '</p>');
    }
    // otherwise, show the error
    else {
        error.css('display', 'block');
    }   
    });
</script>

//HTML
<div class="form-group">
   <label>ID Number:<b style="color:red; ">*</b></label>
   <div class="input-group mb-3">
    <input type="text" name="ID" id="check" class="form-control" required >
    <div class="input-group-append">
     <button class="btn btn-primary" type="button" id="idCheck">Validate</button>
    </div>
   </div>
                   
   <div id="result" style="width:100%; background-color:rgba(58, 114, 59, 0.48); padding:5px; border-radius:5px; color:white; "></div>
   <div id="error" style="width:100%; background-color:rgba(219, 23, 23, 0.48); padding:5px; border-radius:5px; color:white; "></div>
</div>

Example 2: validate rsa id number

# NB--> Language: Perl
use Scalar::Util 'looks_like_number';
sub Validate_IdNum($);
sub GetLuhnDigit($);

#----------------------------------------------------------------#
# Validates the id number being parsed and returns a boolean, 
# If the id number is valid, returns 0, otherwise returns 1
#      PARAMETERS
#                <sIdNo> The id number that needs to be validated
#      EXAMPLES
#               <IN:94091850160a0> <OUTPUT:[0]["Id number cannot contain numeric values"]>
sub Validate_IdNum($)
{
   my $sSubName = (caller(0))[3]; # This is just a reference to the function/subroutine name
   my $bFaulty = 0;

   my $sIdNo = $_[0];
   
   my $iStrLen = length $sIdNo;
   my $LastChar = substr($sIdNo, - 1);

   if ( looks_like_number($sIdNo) == 0 ) {
      $bFaulty = 1;
   }
   else {
      if ( (length $sIdNo) != 13 ) {
         $bFaulty = 1;
      }
      elsif ( substr($sIdNo, - 1) != GetLuhnDigit($sIdNo) ){
         $bFaulty = 1;
      }
   }

   if ($sRetMsg eq "") {
      $sRetMsg = "0";
   }

   return ($sRetMsg,$bFaulty);
}

#----------------------------------------------------------------#
# Used to validate rsa id numbers: (finds expected last digit given the first 12)
# {See the Luhn algorithm: https://en.wikipedia.org/wiki/Luhn_algorithm}
sub GetLuhnDigit($)
{
   my $sSubName = (caller(0))[3]; # This is just a reference to the function/subroutine name
   my $iLastDigit = -1;
   my $s12Digits = $_[0];
   my $iSumOdd = 0;
   my $iEvenDigits = "";

   for (my $i=0;$i<6;$i++) {
     $iSumOdd = $iSumOdd + substr($s12Digits,2 * $i,1);
   }

   for (my $i=0;$i<6;$i++) {
     $iEvenDigits = $iEvenDigits . substr($s12Digits,(2 * $i) + 1,1);
   }

   my $iDblEven = 2 * $iEvenDigits;
   my $iSumDblEvens = 0;
   
   for (my $i = 0; $i < length($iDblEven); $i++) {
      $iSumDblEvens = $iSumDblEvens + substr($iDblEven,$i,1);
   }

   my $iUnitVal = $iDblEven;
   my $iFinalBase = $iSumOdd + $iSumDblEvens;

   $iLastDigit = 10 - substr($iFinalBase, - 1);

   if ($iLastDigit == 10) {
      $iLastDigit = 0;
   }

   return $iLastDigit;
}

Tags:

Misc Example