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
| Input | Result |
|---|---|
| 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"
endphp
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
Mastercard Credit Card
Mastercard — 16 digits starting with 51-55 or 2221-2720
American Express Card
Amex — 15 digits starting with 34 or 37
Any Credit Card
Visa, Mastercard, Amex, Discover, Diners
Email Address
Validates a standard email address format
Email (RFC 5322 Compliant)
RFC 5322 compliant email validation with label length limits
URL (HTTP/HTTPS)
Validates HTTP and HTTPS URLs