Pages

Thursday, September 25, 2008

Credit Card validation

its pretty rare to imagine a person with out knowing the word "Credit Card", If some one is implementing a site which requires "Money Trasactions" then one should have this kind of validation in their code.

The below code is to validate the Input Credit card number.

//Namespace Referenceusing
System.Text.RegulsrExpressions
/// method to validate a credit card number

public static bool validCCNum(string num)
{
// This expression is looking for a series of numbers, which follow the pattern
// for Visa, MC, Discover and American Express. It also allows for dashes between sets of numbers

string pattern = @"^((4\d{3})|(5[1-5]\d{2})|(6011))-?\d{4}-?\d{4}-?\d{4}|3[4,7][\d\s-]{15}$";
Regex match = new Regex(pattern);
return match.IsMatch(num);

}

No comments: