Why would a C# regex credit card validation function validate with invalid values? : <p>I am creating a payment processor WinForms app which of course pushes the results to the gateway API. I am simply trying to validate the credit card number before sending it out. I just need a working 'prototype' so I can finish designing the UI before I dive into learning the Luhn and Base 10 Algorithms so I found the ValidateCreditCard() method in a forum and I just need to get it to be functional.</p>

<pre><code>using System;
using System.Text;
using System.Windows.Forms;

namespace PaymentPlanCalculator
{
    public partial class paymentPlanCalculator : Form
    {
        public paymentPlanCalculator()
        {
            InitializeComponent();
        }

        public void Form1_Load(object sender, EventArgs e)
        {
            /****************************************** //
            /*             DEFINE VARIABLES             //
            /****************************************** */
            //I have a bunch of variables defined here, nothing relevant to the question
        }

        public void Button1_Click(object sender, EventArgs e)
        {
            string creditCardNumber = txtCreditCardNumber.Text;
            if (ValidateCreditCard(creditCardNumber))
            {
                lblCardValid.Text = "VALID!";
            }
            else
            {
                lblCardValid.Text = "INVALID!";
            }

        }

        public bool ValidateCreditCard(string creditCardNumber)
        {
            //Strip any non-numeric values
            creditCardNumber = Regex.Replace(creditCardNumber, @"[^\d]", "");

            //Build your Regular Expression
            Regex expression = new Regex(@"^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$");

            //Return if it was a match or not
            return expression.IsMatch(creditCardNumber);
        }
    }
}

</code></pre>

<p>It appears to work but I have a STRONG feeling that it is validating invalid card numbers as long as they're 16 digits and begin with a 4 or 5.</p>

<p>I have never used regex before and I'm a relatively green C# programmer so I'm assuming that there is something wrong with my implementation as opposed to the function I found online.</p>
