Click here to Skip to main content
15,917,652 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can i validate that the number inserted in a textbox is a valid south african id and if its a male or female
Posted
Updated 26-Mar-18 23:21pm

Have a look here (c# code can be found at the bottom of the page):
http://geekswithblogs.net/willemf/archive/2005/10/30/58561.aspx[^]

Good luck!
 
Share this answer
 
Comments
Monjurul Habib 13-Oct-11 17:06pm    
nice link 5+
The format of a normal South-African ID number is as follows
{YYMMDD}{G}{SSS}{C}{A}{Z}
YYMMDD: Date of birth
G : Gender. 0-4 Female; 5-9 Male.
SSS : Sequence No. for DOB/G combination.
C : Citizenship. 0 SA; 1 Other.
A : Usually 8, or 9 (can be other values)
Z : Control digit.
The following logic explains how the control digit works:

For this explanation I am going to use ID number 860506 5 397 08 3
a) Add all the digits of the ID number in the odd positions (except for the last number, which is the control digit):
8+0+0+5+9+0 = 22
b) Take all the even digits as one number and multiply that by 2:
656378 * 2 = 1312756
c) Add the digits of this number together (in b)
1+3+1+2+7+5+6 = 25
d) Add the answer of C to the answer of A
22+25 = 47
e) Subtract the second character from D from 10, this number should now equal the control character
10-7 = 3 = control character (3)


and thus, if you ever have to validate an ID number this would be the best way to do it ...

Following are more links:
What is a South African ID number made up of?
{Resloved} ID number gender checking
 
Share this answer
 
Comments
Devin Norgarb 25-Jan-18 2:44am    
Thanks so much, 7 years down the line you helped me out.
I have built a working function using PHP
FULL HTML AND PHP CODE WITH EXTRAS

<html>
<body>

	<form method="post" action="<?=$_SERVER['PHP_SELF'];?>">
		<p>Enter the ID Number: <input name="idnumber" type="text" id="idnumber" /> </p>
		<p> <input type="submit" value="Check" /> </p>
	</form>

</body>
</html>
<?php


	Validate($_POST['idnumber']);
	function Validate($idNumber) {
		$correct = true;
		if (strlen($idNumber) !== 13 || !is_numeric($idNumber) ) {
			echo "ID number does not appear to be authentic - input not a valid number";
			$correct = false; die();
		}

		$year = substr($idNumber, 0,2);
		$currentYear = date("Y") % 100;
		$prefix = "19";
		if ($year < $currentYear)
		    $prefix = "20";
	    $id_year = $prefix.$year;

        $id_month = substr($idNumber, 2,2);
        $id_date = substr($idNumber, 4,2);

		$fullDate = $id_date. "-" . $id_month. "-" . $id_year;
		
		if (!$id_year == substr($idNumber, 0,2) && $id_month == substr($idNumber, 2,2) && $id_date == substr($idNumber, 4,2)) {
			echo 'ID number does not appear to be authentic - date part not valid'; 
			$correct = false;
		}
		$genderCode = substr($idNumber, 6,4);
        $gender = (int)$genderCode < 5000 ? "Female" : "Male";

       $citzenship = (int)substr($idNumber, 10,1)  === 0 ? "citizen" : "resident";//0 for South African citizen, 1 for a permanent resident

        $total = 0;
        $count = 0;
	    for ($i = 0;$i < strlen($idNumber);++$i)
	    {
		    $multiplier = $count % 2 + 1;
		    $count ++;
		    $temp = $multiplier * (int)$idNumber{$i};
		    $temp = floor($temp / 10) + ($temp % 10);
		    $total += $temp;
	    }
	    $total = ($total * 9) % 10;

	    if ($total % 10 != 0) {
	        echo 'ID number does not appear to be authentic - check digit is not valid';
		    $correct = false;
	    }

        if ($correct){
            echo nl2br( "\nSouth African ID Number:   ". $idNumber .
                '  Birth Date:   ' . $fullDate.
                '  Gender:  ' . $gender .
                '  SA Citizen:  ' . $citzenship);
        }
	}
 
Share this answer
 
Comments
Richard MacCutchan 27-Mar-18 5:38am    
SEVEN years too late.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900