C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Credit card validation


Posted Date: 10 Oct 2008    Resource Type: Code Snippets    Category: Validations

Posted By: Vishnu K J       Member Level: Gold
Rating:     Points: 10



This code shows how to validate credit card entry..


/// <summary>
/// Validates a credit card number using the standard Luhn/mod10
/// validation algorithm.
/// </summary>
/// <param name="cardNumber">Card number, with or without
/// punctuation</param>
/// <returns>True if card number appears valid, false if not
/// </returns>
public bool IsCreditCardValid(string cardNumber)
{
const string allowed = "0123456789";
int i;

StringBuilder cleanNumber = new StringBuilder();
for (i = 0; i < cardNumber.Length; i++)
{
if (allowed.IndexOf(cardNumber.Substring(i, 1)) >= 0)
cleanNumber.Append(cardNumber.Substring(i, 1));
}
if (cleanNumber.Length < 13 || cleanNumber.Length > 16)
return false;

for (i = cleanNumber.Length + 1; i <= 16; i++)
cleanNumber.Insert(0, "0");

int multiplier, digit, sum, total = 0;
string number = cleanNumber.ToString();

for (i = 1; i <= 16; i++)
{
multiplier = 1 + (i % 2);
digit = int.Parse(number.Substring(i - 1, 1));
sum = digit * multiplier;
if (sum > 9)
sum -= 9;
total += sum;
}
return (total % 10 == 0);
}




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Credit card validation  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Check for whether atleast one Item of Grid is checked or not
Previous Resource: Client Validation - JavaScript : A tricky one
Return to Discussion Resource Index
Post New Resource
Category: Validations


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

budget conference call

Contact Us    Privacy Policy    Terms Of Use