title: Which algorithm does CreditCardAttribute use for credit card number format validation
body: <p>.NET 4.5 includes a new validation attribute named as <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.creditcardattribute.aspx" rel="nofollow"><code>CreditCardAttribute</code></a> and this attribute specifies that a data field value is a credit card number. When I decompile the assembly which contains this class, I can see the following code for the credit card number validation:</p>
  
  <pre><code>public override bool IsValid(object value)
  {
    if (value == null)
    {
      return true;
    }
    string text = value as string;
    if (text == null)
    {
      return false;
    }
    text = text.Replace("-", "");
    text = text.Replace(" ", "");
    int num = 0;
    bool flag = false;
    foreach (char current in text.Reverse&lt;char&gt;())
    {
      if (current &lt; '0' || current &gt; '9')
      {
        return false;
      }
      int i = (int)((current - '0') * (flag ? '\u0002' : '\u0001'));
      flag = !flag;
      while (i &gt; 0)
      {
        num += i % 10;
        i /= 10;
      }
    }
    return num % 10 == 0;
  }
  </code></pre>
  
  <p>Does anybody know which algorithm is applied here to validate the number format? Luhn's algorithm? Also, is this an ISO standard? Finally, do you think that this is the right and 100% correct implementation?</p>
  
  <p>MSDN doesn't provide much information about this. In fact, they have the wrong information as below:</p>
  
  <blockquote>
    <h2>Remarks</h2>
    
    <p>The value is validated using a regular expression. The class does not
    validate that the credit card number is valid for purchases, only that
    it is well formed.</p>
  </blockquote>