Identity
Saudi National ID Shape Regex
Saudi Arabia issues ten-digit identity numbers, where a leading 1 is used for citizens' national IDs and a leading 2 for residents' Iqama numbers. This pattern checks that the input is exactly ten digits and starts with one of those two values, which suits KYC screens, delivery apps, telecom registration and HR systems operating in the Kingdom. Because it is simple, it ports cleanly to every regex engine. The honest limit is the final check digit, which follows a Luhn-style calculation that a regex cannot perform, so add that check separately.
Pattern (JavaScript)
/^[12][0-9]{9}$/Starts at the start of the line/string, then one of the characters 12, then a digit, exactly 9 times, then the end of the line/string.
Limit: Checks the shape only; it cannot tell whether a document is real or valid.
Examples
Matches
- ✓ 1012345678
- ✓ 2098765432
- ✓ 1234567890
Does not match
- ✗ 3012345678
- ✗ 101234567
- ✗ 10123456789
Try it
0 matches (JavaScript engine)
1012345678
2098765432
1234567890
3012345678
101234567
10123456789Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^[12][0-9]{9}$/g;
const text = "1012345678";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^[12][0-9]{9}$')
text = '1012345678'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^[12][0-9]{9}$/';
$text = '1012345678';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^[12][0-9]{9}$");
String text = "1012345678";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[12][0-9]{9}$`)
text := "1012345678"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^[12][0-9]{9}$");
var text = "1012345678";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^[12][0-9]{9}$").unwrap();
let text = "1012345678";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^[12][0-9]{9}$/
text = '1012345678'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- How can I tell a citizen ID from an Iqama number?
- Look at the first digit: 1 usually indicates a Saudi citizen and 2 a resident. If you need them separately, use ^1[0-9]{9}$ and ^2[0-9]{9}$ as two patterns.
- Why is the pattern anchored?
- Anchors make sure the whole field is exactly ten digits. Without them, a search would also succeed on a longer number that merely contains ten digits starting with 1 or 2.
- Does this pattern handle Arabic-Indic digits?
- No, it only accepts ASCII digits 0 to 9. Convert Arabic-Indic digits to ASCII before testing if your users may type them on Arabic keyboards.