Quick answer
The regex for an Indian PIN code is ^[1-9]\d{2}\s?\d{3}$: six digits where the first is 1 to 9, with an optional space after the third digit as some addresses write it. It matches 110001 (New Delhi), 400001 (Mumbai) and 560 001 (Bengaluru), and rejects codes starting with 0, five or seven digits, and hyphens. Remove the space before storing.
- Tested examples
- Token-by-token breakdown
- Code in 5 languages
- Live tester
How to use the India PIN code regex
- 1Copy the pattern with Copy pattern in the tester, or take a ready-made snippet for your language from the code section below.
- 2Check the sample lines in Test string: every line the India PIN code regex accepts is highlighted, rejected lines stay plain.
- 3Paste your own values one per line to see which pass, and read Explanation for what each part of the pattern does.
- 4Open the Code tab for JavaScript, Python, Go, C#, PHP or Dart with the pattern translated and escaped.
- 5Validate again on the server: client-side checks give users quick feedback, not security.
How does the India PIN code regex work?
Read it left to right. Each piece below matches one part of the input:
^[1-9]The first digit, the postal region, from 1 to 9. No PIN code starts with 0.\d{2}The next two digits: the sub-region and the sorting district.\s?An optional space, as in 560 001.\d{3}The last three digits identify the delivery post office.$End of the string.
Which strings does the India PIN code regex match?
The whole input must match, from the first character to the last. Every row below is checked against the pattern by our automated test suite before each release.
| Input | Matches? | Why |
|---|---|---|
110001 | Yes | New Delhi GPO. |
400001 | Yes | Mumbai GPO. |
560 001 | Yes | Bengaluru, written with a space. |
700019 | Yes | Kolkata. |
011001 | No | PIN codes never start with 0. |
11000 | No | Five digits. |
1100011 | No | Seven digits. |
110-001 | No | A hyphen instead of a space. |
ABCDEF | No | Letters. |
What do the digits of a PIN code mean?
India Post introduced the Postal Index Number in 1972. Reading left to right:
| Digits | Meaning | Example (110001) |
|---|---|---|
| 1st | Postal region (1–8 geographic, 9 for the Army Postal Service) | 1: Delhi, Haryana, Punjab, Himachal Pradesh, Jammu and Kashmir |
| 2nd | Sub-region | 1: Delhi |
| 3rd | Sorting district | 0 |
| Last 3 | Delivery post office | 001: New Delhi GPO |
That structure is why [1-9] is the only constraint the regex puts on individual digits: everything else depends on which offices exist, which changes over time.
How do I check that a PIN code exists?
The format check cannot tell you whether a post office uses that number. India Post publishes the full PIN code directory through the Open Government Data platform, and several free APIs return the post office, district and state for a code, which also lets you auto-fill the city and state fields in a checkout form.
On mobile, use inputmode="numeric" and autocomplete="postal-code", keep the field as text, and strip the optional space with pin.replace(/\s/g, "") before saving.
How do I use the India PIN code regex in JavaScript, Python, Go, C# and PHP?
The pattern is translated for each engine rather than pasted as is: named groups, anchors, escapes, shorthand classes and flags are adjusted so each snippet behaves as closely to the JavaScript version as its engine allows. Differences that remain (Go's RE2 has no lookarounds or backreferences, for example) are called out under each one. The Code tab in the tester above also exports Dart, and updates as you edit the pattern.
JavaScript and TypeScript
const regex = /^[1-9]\d{2}\s?\d{3}$/;
function isValid(value) {
return regex.test(value);
}
console.log(isValid("110001"));Python
import re
pattern = re.compile(r"^[1-9][0-9]{2}\s?[0-9]{3}\Z")
def is_valid(value: str) -> bool:
return pattern.search(value) is not None
print(is_valid("110001"))- \d and \w are written as [0-9] and [A-Za-z0-9_] because in Python they also match digits and letters from other scripts.
- JavaScript's $ only matches at the very end of the string. Python's $ also matches before a trailing newline, so the pattern uses \Z instead.
Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[1-9]\d{2}\s?\d{3}$`)
fmt.Println(re.MatchString("110001"))
}- Go's \s only matches space, \t, \n, \f and \r; JavaScript's also matches the non-breaking space and other Unicode spaces.
C#
using System;
using System.Text.RegularExpressions;
// The timeout stops a pathological input from hanging the thread (ReDoS).
var regex = new Regex(@"^[1-9][0-9]{2}\s?[0-9]{3}\z", RegexOptions.None, TimeSpan.FromSeconds(1));
Console.WriteLine(regex.IsMatch("110001"));- \d and \w are written as [0-9] and [A-Za-z0-9_] because in .NET they also match digits and letters from other scripts.
- JavaScript's $ only matches at the very end of the string. .NET's $ also matches before a trailing newline, so the pattern uses \z instead.
PHP
<?php
$pattern = '/^[1-9]\d{2}\s?\d{3}$/D';
function isValid(string $value, string $pattern): bool
{
return preg_match($pattern, $value) === 1;
}
var_dump(isValid('110001', $pattern));- PCRE's \s matches ASCII whitespace unless Unicode properties (UCP) are on; JavaScript's also matches the non-breaking space and other Unicode spaces.
- The D modifier makes $ match only at the very end, like JavaScript (PCRE's $ also matches before a trailing newline).
Which related regex patterns are useful?
- Regex for US ZIP Codes: Five-digit ZIP codes with an optional ZIP+4 extension, like 90210-1234.
- Regex for UK Postcodes: All six UK postcode formats, with or without the space, including SW1A 1AA.
- Regex for Phone Numbers (E.164 Format): International numbers in E.164: a plus, a country code and up to 15 digits in total.
- Regex for Numbers Only: Digits only, plus variants for signs, decimals and thousands separators.
Browse all 25 patterns in the regex pattern library, or build your own in the regex tester.
Frequently asked questions
Last updated by the Playxoft engineering team.