Finance
Visa card number shape Regex
Visa card numbers start with the digit 4. This pattern accepts the classic 16 digit form, optionally split into four groups of four by spaces or dashes, and the older 13 digit form written as one unbroken run. It is handy when a payment form wants to show a Visa logo as the user types, or when you need to route card-shaped input to network-specific handling. It does not cover the newer 19 digit Visa numbers, and like every card regex it only checks the shape. It does not compute the Luhn checksum and cannot confirm the card was ever issued.
Pattern (JavaScript)
/^4(?:\d{12}|\d{3}(?:[ -]?\d{4}){3})$/Starts at the start of the line/string, then the character "4", then (exactly 12 digits OR exactly 3 digits, then (one of the characters -, optionally (zero or one time), then exactly 4 digits), exactly 3 times), then the end of the line/string.
Limit: Checks the shape only, not the Luhn checksum or whether the card exists.
Examples
Matches
- ✓ 4111111111111111
- ✓ 4111 1111 1111 1111
- ✓ 4222222222222
- ✓ 4012-8888-8888-1881
Does not match
- ✗ 5555555555554444
- ✗ 4111 1111 1111
- ✗ 41111111111111111
- ✗ 3111 1111 1111 1111
Try it
0 matches (JavaScript engine)
4111111111111111
4111 1111 1111 1111
4222222222222
4012-8888-8888-1881
5555555555554444
4111 1111 1111
41111111111111111
3111 1111 1111 1111Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^4(?:\d{12}|\d{3}(?:[ -]?\d{4}){3})$/g;
const text = "4111111111111111";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^4(?:\d{12}|\d{3}(?:[ -]?\d{4}){3})$')
text = '4111111111111111'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^4(?:\d{12}|\d{3}(?:[ -]?\d{4}){3})$/';
$text = '4111111111111111';
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("^4(?:\\d{12}|\\d{3}(?:[ -]?\\d{4}){3})$");
String text = "4111111111111111";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^4(?:\d{12}|\d{3}(?:[ -]?\d{4}){3})$`)
text := "4111111111111111"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^4(?:\d{12}|\d{3}(?:[ -]?\d{4}){3})$");
var text = "4111111111111111";
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"^4(?:\d{12}|\d{3}(?:[ -]?\d{4}){3})$").unwrap();
let text = "4111111111111111";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^4(?:\d{12}|\d{3}(?:[ -]?\d{4}){3})$/
text = '4111111111111111'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why does every Visa number start with 4?
- The first digit of a card number is the major industry identifier, and Visa was assigned the range starting with 4. The pattern therefore requires a leading 4 before anything else.
- Can the 13 digit Visa form contain spaces?
- Not in this pattern. Thirteen digit numbers do not split evenly into groups of four, so the pattern only accepts them as one continuous run. Strip separators first if your input may contain them.
- Is 4111 1111 1111 1111 a real card?
- No. It is a widely published test number that payment sandboxes accept for development. Use it in examples and tests instead of anything that could belong to a real person.
Related patterns
- American Express card number shape^3[47]\d{2}[ -]?\d{6}[ -]?\d{5}$
- Credit card number (generic shape)^(?:\d[ -]?){12,18}\d$
- Mastercard number shape^(?:5[1-5]\d{2}|222[1-9]|22[3-9]\d|2[3-6]\d{2}|27[01]\d|2720)(?:[ -]?\d{4}){3}$
- Accounting negative amount in parentheses^\((?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{2})?\)$