Pakistan Special
Pakistan CNIC Number Regex
The Computerized National Identity Card number used in Pakistan has thirteen digits and is usually written in three dashed groups, as in 12345-1234567-1. This pattern enforces exactly that layout: five digits, a dash, seven digits, a dash and one final digit, anchored at both ends. It is a good first check on sign-up and KYC forms because it catches missing digits, extra digits and misplaced dashes before anything is sent to a server. It checks the 13-digit shape only; a regex cannot confirm that a CNIC is real or belongs to a person, so official checks must happen elsewhere.
Pattern (JavaScript)
/^\d{5}-\d{7}-\d$/Starts at the start of the line/string, then exactly 5 digits, then the character "-", then exactly 7 digits, then the character "-", then a digit (0-9), then the end of the line/string.
Limit: Checks the 13-digit shape only; a regex cannot confirm that a CNIC is real or belongs to a person.
Examples
Matches
- ✓ 12345-1234567-1
- ✓ 35202-0000000-3
- ✓ 42101-1111111-0
Does not match
- ✗ 1234512345671
- ✗ 12345-123456-1
- ✗ 12345-1234567-12
Try it
0 matches (JavaScript engine)
12345-1234567-1
35202-0000000-3
42101-1111111-0
1234512345671
12345-123456-1
12345-1234567-12Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^\d{5}-\d{7}-\d$/g;
const text = "12345-1234567-1";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^\d{5}-\d{7}-\d$')
text = '12345-1234567-1'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^\d{5}-\d{7}-\d$/';
$text = '12345-1234567-1';
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("^\\d{5}-\\d{7}-\\d$");
String text = "12345-1234567-1";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^\d{5}-\d{7}-\d$`)
text := "12345-1234567-1"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^\d{5}-\d{7}-\d$");
var text = "12345-1234567-1";
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"^\d{5}-\d{7}-\d$").unwrap();
let text = "12345-1234567-1";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^\d{5}-\d{7}-\d$/
text = '12345-1234567-1'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Does this pattern accept a CNIC typed without dashes?
- No. It requires both dashes in the right places. Use the separate CNIC-without-dashes pattern, or remove dashes in code and check for thirteen digits.
- Can a regex tell whether a CNIC is genuine?
- No. The pattern checks the 13-digit shape only; a regex cannot confirm that a CNIC is real or belongs to a person. That needs an official record lookup by an authorised service.
- How do I accept both dashed and plain forms?
- Use ^\d{5}-?\d{7}-?\d$ to make both dashes optional. Normalise the value to one form before saving it so duplicates are easy to detect.