Credit Card Validation

G

Grant

Does any one know how to check the algorithm of the credit card number that
was entered in the text box? I want to be able to make sure the users enter
correct credit card number since we will process it manually via phone. I
want this to be done on the server side behind the code in ASP.Net. Thanks
 
S

S. Justin Gengo

I have a credit card validation sample on my web site in the code library:
www.aboutfortunate.com

It's titled: Regular expression credit card validation. I haven't
implemented a way to search the database yet, but if you set the first drop
down list to: Web or Windows, and the second drop down to: JavaScript it
will limit the library to just that entry.

--
S. Justin Gengo
Web Developer / Programmer

Free Code Library At:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
 
V

vMike

Here is an access function for generic card testing which might help you
also.

Function CreditCardCheckDigit(strCreditCard As String) As Boolean
Dim intLength As Integer
Dim intEvenSum As Integer
Dim intOddSum As Integer
Dim i As Integer
CreditCardCheckDigit = False
On Error GoTo Errorhandler
intEvenSum = 0
intOddSum = 0
intLength = Len(strCreditCard)
If intLength Mod 2 = 1 Then strCreditCard = "0" & strCreditCard ' for
amex
For i = 1 To intLength - 1
If i Mod 2 = 0 Then
intEvenSum = intEvenSum + CInt(Mid(strCreditCard, i, 1))
ElseIf CInt(Mid(strCreditCard, i, 1)) = 9 Then
intOddSum = intOddSum + 9
Else
intOddSum = intOddSum + CInt(Mid(strCreditCard, i, 1)) * 2 Mod 9
End If
Next i
If Right(CStr(1000 - (intEvenSum + intOddSum)), 1) =
CInt(Right(strCreditCard, 1)) Then
CreditCardCheckDigit = True
End If


Exit Function
Errorhandler:
MsgBox "Credit Card Check Digit function failed."

End Function
 
R

Ron C.

Someone has already posted a VB.NET version, so no need to go there. I have
a C# version somewhere, if that is a need.
I would greatly appreciate the C# version :))
Ron
 
C

Cowboy \(Gregory A. Beamer\)

Ron:

I have tweaked the Mod10 to make it a bit cleaner (the original was
converted from a VB project):

public bool CheckMod10(string cardNumber)
{
char[] cardNums = cardNumber.Trim().ToCharArray();
int cardLength = cardNums.Length;
bool lengthCardEven = ((cardNums.Length%2)==0);
int checkValue=0;
int currentValue=0;

for(int counter=0;counter<cardNums.Length;counter++)
{

if((((counter%2)==0)&&(lengthCardEven))||(((counter%2)==1)&&(!lengthCardEven
)))
currentValue = Convert.ToInt32(cardNums[counter].ToString())*2;
else
currentValue = Convert.ToInt32(cardNums[counter].ToString());

if(currentValue>=10)
currentValue -= 9;

checkValue += currentValue;
}

return checkValue%10==0 ? true : false;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top