Validating South African ID Numbers

by Donovan Olivier August 25, 2010 11:56AM

I recently found a nice blog post explaining how to validate South Aftican 13-digit ID numbers, so here's my take on the code:

namespace Imaginary
{
    using System;
    using System.Linq;
    using System.Text;

    public enum Gender
    {
        Unknown,
        Male,
        Female
    }

    public class IdentityInfo
    {
        public IdentityInfo(string identityNumber)
        {
            this.Initialize(identityNumber);
        }

        public string IdentityNumber { get; private set; }

        public DateTime BirthDate { get; private set; }

        public Gender Gender { get; private set; }

        public bool IsSouthAfrican { get; private set; }

        public bool IsValid { get; private set; }

        private void Initialize(string identityNumber)
        {
            this.IdentityNumber = (identityNumber ?? string.Empty).Replace(" ", "");
            if (this.IdentityNumber.Length == 13)
            {
                var digits = new int[13];
                for (int i = 0; i < 13; i++)
                {
                    digits[i] = int.Parse(this.IdentityNumber.Substring(i, 1));
                }
                int control1 = digits.Where((v, i) => i % 2 == 0 && i < 12).Sum();
                string second = string.Empty;
                digits.Where((v, i) => i % 2 != 0 && i < 12).ToList().ForEach(v => 
                                                                 second += v.ToString());
                var string2 = (int.Parse(second) * 2).ToString();
                int control2 = 0;
                for (int i = 0; i < string2.Length; i++)
                {
                    control2 += int.Parse(string2.Substring(i, 1));
                }
                var control = (10 - ((control1 + control2) % 10)) % 10;
                if (digits[12] == control)
                {
                    this.BirthDate = DateTime.ParseExact(this.IdentityNumber
                                                       .Substring(0, 6), "yyMMdd", null);
                    this.Gender = digits[6] < 5 ? Gender.Female : Gender.Male;
                    this.IsSouthAfrican = digits[10] == 0;
                    this.IsValid = true;
                }
            }
        }
    }
}

To use it, simply instantiate a new IdentityInfo, passing in the ID number.


Tags: ,

Blog

The Perpetrator

perpetrator

Donovan Olivier

software developer, architect, husband, father, son, brother, uncle, nephew, sinistral, enquirer, skeptic, cynic, anarchist, contrarian, infidel, dissident, rebel, troublemaker, dissenter, renegade.

Noise

i know one shld look at beer studies undertaken by the institute of brewing & distillery with a critical eye, but... http://t.co/hOtxdgox
planetdonovan 4 hours ago

is there any way to ensure that the electronics you buy are manufactured by free range humans? http://t.co/smfg8lSg
planetdonovan 7 hours ago

@planetdonovan ha ha I thought the same when that guy hit him.
LBugnion 1 day ago

@jalpf @LBugnion it is hard, however, to see him getting beaten up... c'mon jack!
planetdonovan 1 day ago

#FF - @thisisinterval @greyholed @planetdonovan @Sonicsgate
adrianshannon 2 days ago

this is why most f1 cars are gonna look weird this year http://t.co/zoObHpUg
planetdonovan 3 days ago

new ferrari f2012 nose is freaky! http://t.co/kwBktVib
planetdonovan 3 days ago

random site of the day http://t.co/wZYZg0FI
planetdonovan 3 days ago

computing achievements http://t.co/JIsYZcjB (via @gizmodo)
planetdonovan 3 days ago

RT @LiveScience: Newfound Alien Planet is Best Candidate Yet to Support Life, Scientists Say http://t.co/BKx8ToBT
planetdonovan 3 days ago

well, this is bound to be controversial http://t.co/j724XCTP (via @carolynporco)
planetdonovan 3 days ago

opposing the death penalty seems to be the unpopular point of view in south africa.
planetdonovan 4 days ago

started my day with a colossal database screw up - things can only get better from here.
planetdonovan 5 days ago

conspiracy theory of the day http://t.co/qy14T7Xx (clues: "cancer industry", "medical mafia")
planetdonovan 6 days ago