You are here
Home > Programming > South African ID Number Validation in C#

South African ID Number Validation in C#

South African ID Number Validation – There are many reasons a developer in South Africa would need to validate a South African ID number. For example to validate the input on an Online order form or as a form of identification for a loan etc. But how exactly do you do this? I’m sure that many of you have already found the Web Service to do this, but here is my take on it. A South African ID Number is made up as follows:

YYMMDDGSSSCAZ

YYMMDD : Date of birth (DOB)
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 but can be other values
Z : Calculated control (check) digit

For more information, documentation and info on how the control digit is calculated, refer to SA ID Validator Web Page. What I did was to create a small application in Visual Studio 2010 (which is also available for download at the end of this post).

South African ID Number Validation – How To Do It
  • In Visual Studio 2010, I created a simple Windows Forms Project and designed the form to include a text entry for the ID Number, a Validate Button and an Output field.

South African ID Validation Test Form

  • In the Visual Studio Solution, Right Click on “Service References” and select “Add Service Reference”

South African ID Number Validation

  • At the bottom of the Add Service Reference window, click on “Advanced”.

South African ID Number Validation

  • In the Service Reference Settings screen, click on “Add Web Reference”. (Yes, we are adding a plain old Web Service Reference)

South African ID Number Validation

  • In the Add Web Reference screen, specify the URL as follows: http://xml-fx.com/services/SAIDValidator/SAIDValidator.asmx and click on the green arrow to the right of the URL field.
  • Please note that this Web Service doesn’t seem to be active anymore. Instead consider using this instead from Sage Pay (you might need their SDK though to use it). South African ID validation technical guide

  • Alternatively have a look at the pbVerify Verification API.

    If anyone comes across another public web service to verify South African ID numbers, please share in the comments below.

South African ID Number Validation

  • The documentation will be displayed in the screen and the saidvalidator service will be shown as found. Give the Web Reference a suitable name and click on “Add Reference”. The Visual Studio solution should now look as follows:

South African ID Number Validation

From here on, the code is fairly simple. Just add a using statement (Imports for the VB.Net folks out there):

[sourcecode language=”csharp”]using TestSAIDValidation.ValidateSAID;[/sourcecode]

Add the following code in the button click event:

[sourcecode language=”csharp”]
string sSAID = txtSAID.Text.Trim();
TestSAIDValidation.ValidateSAID.saidType oReturn;

SAIDValidator oValidate = new SAIDValidator();
// authToken – Authentication token string. Currently can be set to any value including null
oReturn = oValidate.ValidateIdString(“authToken”, sSAID);

bool blnValidated = oReturn.Valid;

if (blnValidated)
{
saidTypeOutput oOutput = new saidTypeOutput();
oOutput = oReturn.output;
string DOB = oOutput.DOB.ToString(“dd MMMM yyyy”);
string Citizen = oOutput.Citizen.ToString();
string Gender = oOutput.Gender.ToString();
string Output = String.Format(“Valid SA ID Number{0}Date of Birth: ” + DOB + “{0}Citizen: ” + Citizen + “{0}Gender: ” + Gender, Environment.NewLine);
txtOutput.Text = Output;
}
else
{
txtOutput.Text = “Invalid SA ID Number”;
}
[/sourcecode]

You should now have a working South African ID validation service that you can incorporate into your own projects using your own best practices. Please note that the code and attached solution does not necessarily denote best practice. It is simply a solution to illustrate the Validation of a South African ID number. As always, any comments are welcome.

Download Visual Studio 2010 Solution.

Image: EWN

Dirk Strauss
Dirk is a Software Developer and Microsoft MVP from South Africa. He loves all things Technology and is slightly addicted to Twitter and Jimi Hendrix. Apart from writing code, he also enjoys writing human readable articles. "I love sharing knowledge and connecting with people from around the world. It's the diversity that makes life so beautiful." Dirk feels very strongly that pizza is simply not complete without Tabasco, that you can never have too much garlic, and that cooking the perfect steak is an art he has yet to master.

Similar Articles

Top