THE EXPERIENCED

Software Engineer - Back-End Developer / Front-End Developer

A regex that matches all credit cards

This is a regular expression that validates all credit card types including VISA, MASTER, AMEX and DISCOVERY!
^((4\d{3})|(5[1-5]\d{2})|(6011)|(34\d{1})|(37\d{1}))-?\d{4}-?\d{4}-?\d{4}|3[4,7][\d\s-]{15}$

ASP.NET WebForms

<asp:RegularExpressionValidator ID="CreditCardValidator" runat="server" ControlToValidate="CreditCardNumber"
    Display="Static" ErrorMessage="Please enter valid card number" ValidationGroup="CustomSignup" Text="*"
    ValidationExpression="^((4\d{3})|(5[1-5]\d{2})|(6011)|(34\d{1})|(37\d{1}))-?\d{4}-?\d{4}-?\d{4}|3[4,7][\d\s-]{15}$" />
ASP.NET Core Razor Pages

[RegularExpression(@"^((4\d{3})|(5[1-5]\d{2})|(6011)|(34\d{1})|(37\d{1}))-?\d{4}-?\d{4}-?\d{4}|3[4,7][\d\s-]{15}$")]
[Required]
public string CreditCardNumber { get; set; }