Visa Card Number Regex

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

Validate Visa card numbers: starts with 4, 13 or 16 digits total.

Test This Pattern →

Pattern Breakdown

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

  • ^ - Start of string
  • $ - End of string
  • ? - Optional (zero or one)
  • [...] - Character class
  • (...) - Capturing group
  • (?:...) - Non-capturing group
  • {n,m} - Quantifier (n to m times)

Examples

Input Result Explanation
4111111111111111 ✓ Match Visa 16-digit
4111111111111 ✓ Match Visa 13-digit

Code Examples

JavaScript

JavaScript
const pattern = /^4[0-9]{12}(?:[0-9]{3})?$/;
const text = "4111111111111111";

// Test if string matches
if (pattern.test(text)) {
  console.log("Valid!");
}

// Extract matches
const matches = text.match(pattern);
console.log(matches);

Python

Python
import re

pattern = r'^4[0-9]{12}(?:[0-9]{3})?$'
text = "4111111111111111"

# Test if string matches
if re.match(pattern, text):
    print("Valid!")

# Find all matches
matches = re.findall(pattern, text)
print(matches)

PHP

PHP
<?php
$pattern = '/^4[0-9]{12}(?:[0-9]{3})?$/';
$text = "4111111111111111";

// Test if string matches
if (preg_match($pattern, $text)) {
    echo "Valid!";
}

// Find all matches
preg_match_all($pattern, $text, $matches);
print_r($matches);
?>

Java

Java
import java.util.regex.*;

String pattern = "^4[0-9]{12}(?:[0-9]{3})?$";
String text = "4111111111111111";

Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(text);

if (m.matches()) {
    System.out.println("Valid!");
}

Use Cases

Common applications: Visa-specific validation

Frequently Asked Questions

What is the regex pattern for visa card number regex?
The regex pattern is: ^4[0-9]{12}(?:[0-9]{3})?$. Validate Visa card numbers: starts with 4, 13 or 16 digits total.
How do I use the visa card number regex pattern in JavaScript?
In JavaScript: const regex = new RegExp('^4[0-9]{12}(?:[0-9]{3})?$'); or const regex = /^4[0-9]{12}(?:[0-9]{3})?$/;
Does this pattern work in Python?
Yes, use: import re; pattern = re.compile(r'^4[0-9]{12}(?:[0-9]{3})?$') in Python. Most regex patterns are cross-language compatible.
What are common use cases for visa card number regex?
Visa-specific validation

Related Regex Patterns

More Regex Tools

GetDevUtils offers a suite of developer tools including: