Credit Card Validation Question

  • Thread starter Sparky Arbuckle
  • Start date
S

Sparky Arbuckle

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
 
K

Kevin Spencer

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.
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top