Groups | Blog | Home
all groups > asp.net > april 2005 >

asp.net : Credit Card Validation Question


Sparky Arbuckle
4/6/2005 11:10:15 PM
The code below will pass a credit card as being valid if it contains
dashes, spaces, and also alphanumerics. How can I check for only dashes
and spaces and return invalid when the user inputs alphanumerics?
Thanks.


Sub ValidateCCNumber( s As Object, e As ServerValidateEventArgs )
Dim intCounter As Integer
Dim strCCNumber As String
Dim blnIsEven As Boolean = False
Dim strDigits As String = ""
Dim intCheckSum As Integer = 0

'Strip away everything except numerals
FOR intCounter = 1 To Len( e.Value )
IF IsNumeric( MID( e.Value, intCounter, 1 ) ) THEN
strCCNumber = strCCNumber & MID( e.Value, intCounter, 1 )
END IF
NEXT

'IF nothing left, then fail
IF Len( strCCNumber ) = 0 THEN
e.IsValid = False
ELSE

'Double every other digit
FOR intCounter = Len( strCCNumber ) To 1 Step -1
IF blnIsEven THEN
strDigits = strDigits & cINT( MID( strCCNumber, intCounter, 1 )
) * 2
ELSE
strDigits = strDigits & cINT( MID( strCCNumber, intCounter, 1 )
)
END IF
blnIsEven = ( NOT blnIsEven )
NEXT

'Calculate CheckSum
FOR intCounter = 1 To Len( strDigits )
intCheckSum = intCheckSum + cINT( MID( strDigits, intCounter, 1
) )
NEXT

'Assign results
e.IsValid = (( intCheckSum Mod 10 ) = 0 )

END IF
END Sub
Brock Allen
4/7/2005 5:27:21 AM
Look into using regular expressions:

http://www.codeproject.com/aspnet/creditcardvalidator.asp

http://www.codeproject.com/aspnet/CreditCardValidator2.asp

http://www.evolt.org/article/Validating_a_Credit_Card_Number_with_JavaScript/17/24700/

-Brock
DevelopMentor
http://staff.develop.com/ballen



[quoted text, click to view]


Kevin Spencer
4/7/2005 9:03:30 AM
Search Google for a Regular Expression that validates a Credit Card. There
are plenty.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.

[quoted text, click to view]

AddThis Social Bookmark Button