luhn

Validating Israeli ID number


I'm looking for a clean and efficient way to validate an Israeli ID number.

It's basically an implementation of the Luhn algorithm on a 9 digits number.

Note: This question is here for the community because it wasn't on stack overflow yet. You can add answers in different coding languages.


Solution

  • Here's an efficient way to implement it in C# (link):

    public static bool IsValidIsraeliID(string israeliID)
        {
            if (israeliID.Length != 9)
                return false;
    
            long sum = 0;
    
            for (int i = 0; i < israeliID.Length; i++)
            {
                var digit = israeliID[israeliID.Length - 1 - i] - '0';
                sum += (i % 2 != 0) ? GetDouble(digit) : digit;
            }
    
            return sum % 10 == 0;
    
            int GetDouble(long i)
            {
                switch (i)
                {
                    case 0: return 0;
                    case 1: return 2;
                    case 2: return 4;
                    case 3: return 6;
                    case 4: return 8;
                    case 5: return 1;
                    case 6: return 3;
                    case 7: return 5;
                    case 8: return 7;
                    case 9: return 9;
                    default: return 0;
                }
            }
        }