DD
DevDash

Regex for Visa Credit Card

Regex Pattern

^4[0-9]{12}(?:[0-9]{3})?$

Visa card — starts with 4, 13 or 16 digits total

Quick Answer

The regex pattern for visa credit card is `^4[0-9]{12}(?:[0-9]{3})?$`. Visa card — starts with 4, 13 or 16 digits total. This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.

Test Examples

InputResult
4111111111111111✓ Matches
4012888888881881✓ Matches
4222222222222✓ Matches
5111111111111111✗ No match
411111111111111✗ No match
4a111111111111✗ No match

Code Examples

javascript

const regex = /^4[0-9]{12}(?:[0-9]{3})?$/;
const isValid = regex.test(value);

python

import re
pattern = r'^4[0-9]{12}(?:[0-9]{3})?$'
if re.match(pattern, value):
    print("valid")

ruby

pattern = /^4[0-9]{12}(?:[0-9]{3})?$/
if value =~ pattern
  puts "valid"
end

php

if (preg_match('/^4[0-9]{12}(?:[0-9]{3})?$/', $value)) {
    echo "valid";
}

java

String pattern = "^4[0-9]{12}(?:[0-9]{3})?$";
boolean isValid = value.matches(pattern);

Frequently Asked Questions

Related Regex Patterns

Want API access + no ads? Pro coming soon.