'Saudi Iqama/National Identity number field validation

Can someone help me with the criteria for field validation for Saudi iqama/National Identification number?

I know for Nationals it should start with 1 and for expats, it should start with 2 and the length should be 10.Anything else. This is for an application which has already seen some bad incoming data creating bugs, so don't want to take chances. Thanks in advance.



Solution 1:[1]

For (Id Type=’National Id’) ? length = 10 , Id Number must start with ‘1’ in position 1. Multiply every number in an Odd Position (characters in positions 1,3,5..9) by 2. If the multiplication yields a 2 digit number then summate these 2 digits to yield a single digit. Summate the first 9 Characters for the ID number (post multiplication) to yield a 2 digit number (XY). If 10th digit of the ID number equals to “Zero”, then subtract 10 by the digit in position 2 (Y) in the summation total of the first 9 characters to yield a 2 digits (ZN), (N) should be equal to the 10th digit of the ID Number. Else, the 10th digit of the ID Number should be equal to 10 subtracted by the digit in position 2 (Y) in the Summation total of the first 9 Characters.

Solution 2:[2]

Saudi ID Number – 1058529940  
Position 1 2 3 4 5 6 7 8 9 10   
ID Nbr   1 0 5 8 5 2 9 9 4 0   
Multiply 2 0 1 8 1 2 9 9 8   
Summate 40  

Validation Position 10 (value 0) = 10 – Digit 2
of Summation Total (0) = subtraction’s digit 2

e.g. ID Number = 1001244019

Position 1 2 3 4 5 6 7 8 9 10  
ID Nbr   1 0 0 1 2 4 4 0 1 9   
Multiply 2 0 0 1 4 4 8 0 2   
Summate 21   

Validation Position 10 (value 9) = 10 – Digit 2
of Summation Total (1)

Solution 3:[3]

Try validating using regex. ^[1|2]{1}[0-9]{9}$ this worked for me.

or if you are looking for a code that checks whether the id is correct or incorrect check this GitHub repo: https://github.com/alhazmy13/Saudi-ID-Validator. It's written in different programming languages, an it checks both National ID and Residence ID

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Waleed
Solution 2 Martin
Solution 3