DD
DevDash

Regex for 4-Digit PIN

Regex Pattern

^\d{4}$

Four-digit PIN code

Quick Answer

The regex pattern for 4-digit pin is `^\d{4}$`. Four-digit PIN code. This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.

Test Examples

InputResult
1234✓ Matches
0000✓ Matches
9999✓ Matches
123✗ No match
12345✗ No match
abcd✗ No match
12 34✗ No match

Code Examples

javascript

const regex = /^\d{4}$/;
const isValid = regex.test(value);

python

import re
pattern = r'^\d{4}$'
if re.match(pattern, value):
    print("valid")

ruby

pattern = /^\d{4}$/
if value =~ pattern
  puts "valid"
end

php

if (preg_match('/^\d{4}$/', $value)) {
    echo "valid";
}

java

String pattern = "^\\d{4}$";
boolean isValid = value.matches(pattern);

Frequently Asked Questions

Related Regex Patterns

Want API access + no ads? Pro coming soon.