Sakana
Mar 17th, 2004, 08:13 AM
Hi,
I have tried on doing the credit card validation however, it does not work out. What actually went wrong?
VB Script
Function ValidateCreditCard(ByVal cardName As String, _
ByVal cardNo As String, ByVal cardMonth As Integer, _
ByVal cardYear As Integer, ByVal PaymentAmt As String)
'check to ensure that the number is numeric
If Not IsNumeric(cardNo) Then
Session("errorMsg") = "Please enter a valid number for credit card."
Return 1 'If return 1, invalid credit card number
Else
ValidateCCNumber(cardNo)
Session("errorMsg") = "Please enter a valid number for credit card."
End If
Session("errorMsg") = ""
Return 0
End Function
Function ValidateCCNumber(ByVal cardNo)
Dim intCCType
intCCType = GetCCType(cardNo)
Select Case intCCType
Case 1 'Visa
'Validates Length Based on CardType If
' true actually validates number
If (Len(cardNo) = 13 Or Len(cardNo) = 16) And ValidateMod10(cardNo) = True Then
ValidateCCNumber = intCCType 'Valid
Else
ValidateCCNumber = intCCType * -1 'Invalid
End If
Case 2, 3 'Mastercard / Discover
If Len(cardNo) = 16 And ValidateMod10(cardNo) = True Then
ValidateCCNumber = intCCType 'Valid
Else
ValidateCCNumber = intCCType * -1 'Invalid
End If
Case 4 'American Express
If Len(cardNo) = 15 And ValidateMod10(cardNo) = True Then
ValidateCCNumber = intCCType 'Valid
Else
ValidateCCNumber = intCCType * -1 'Invalid
End If
Case 5 'Diners Club
If Len(cardNo) = 14 And ValidateMod10(cardNo) = True Then
ValidateCCNumber = intCCType 'Valid
Else
ValidateCCNumber = intCCType * -1 'Invalid
End If
Case Else
ValidateCCNumber = 0
Return 3
End Select
End Function
Function GetCCType(ByVal cardNo)
If Left(cardNo, 1) = 4 Then
'Visa
GetCCType = 1
ElseIf Left(cardNo, 2) >= 51 And Left(cardNo, 2) <= 55 Then
'Mastercard
GetCCType = 2
ElseIf Left(cardNo, 4) = 6011 Then
'Discover
GetCCType = 3
ElseIf Left(cardNo, 2) = 34 Or Left(cardNo, 2) = 37 Then
'American Express
GetCCType = 4
ElseIf (Left(cardNo, 3) >= 300 And Left(cardNo, 3) <= 305) Or _
Left(cardNo, 2) = 36 Or Left(cardNo, 2) = 38 Then
'Diners Club -- I don't know if this works coz i dun haf one to check it wif
GetCCType = 5
Else
GetCCType = 0
End If
End Function
'This is the actual Mod10
Private Function ValidateMod10(ByVal cardNo)
Dim intCounter, intTotal
Dim strTotal
intTotal = 0
For intCounter = 1 To Len(cardNo)
If (intCounter Mod 2) <> 0 Then
strTotal = strTotal & Mid(cardNo, intCounter, 1) * 2
Else
strTotal = strTotal & Mid(cardNo, intCounter, 1)
End If
Next
For intCounter = 1 To Len(strTotal)
intTotal = intTotal + Mid(strTotal, intCounter, 1)
Next
If (intTotal Mod 10) Then
ValidateMod10 = False
Else
ValidateMod10 = True
End If
End Function
I have tried on doing the credit card validation however, it does not work out. What actually went wrong?
VB Script
Function ValidateCreditCard(ByVal cardName As String, _
ByVal cardNo As String, ByVal cardMonth As Integer, _
ByVal cardYear As Integer, ByVal PaymentAmt As String)
'check to ensure that the number is numeric
If Not IsNumeric(cardNo) Then
Session("errorMsg") = "Please enter a valid number for credit card."
Return 1 'If return 1, invalid credit card number
Else
ValidateCCNumber(cardNo)
Session("errorMsg") = "Please enter a valid number for credit card."
End If
Session("errorMsg") = ""
Return 0
End Function
Function ValidateCCNumber(ByVal cardNo)
Dim intCCType
intCCType = GetCCType(cardNo)
Select Case intCCType
Case 1 'Visa
'Validates Length Based on CardType If
' true actually validates number
If (Len(cardNo) = 13 Or Len(cardNo) = 16) And ValidateMod10(cardNo) = True Then
ValidateCCNumber = intCCType 'Valid
Else
ValidateCCNumber = intCCType * -1 'Invalid
End If
Case 2, 3 'Mastercard / Discover
If Len(cardNo) = 16 And ValidateMod10(cardNo) = True Then
ValidateCCNumber = intCCType 'Valid
Else
ValidateCCNumber = intCCType * -1 'Invalid
End If
Case 4 'American Express
If Len(cardNo) = 15 And ValidateMod10(cardNo) = True Then
ValidateCCNumber = intCCType 'Valid
Else
ValidateCCNumber = intCCType * -1 'Invalid
End If
Case 5 'Diners Club
If Len(cardNo) = 14 And ValidateMod10(cardNo) = True Then
ValidateCCNumber = intCCType 'Valid
Else
ValidateCCNumber = intCCType * -1 'Invalid
End If
Case Else
ValidateCCNumber = 0
Return 3
End Select
End Function
Function GetCCType(ByVal cardNo)
If Left(cardNo, 1) = 4 Then
'Visa
GetCCType = 1
ElseIf Left(cardNo, 2) >= 51 And Left(cardNo, 2) <= 55 Then
'Mastercard
GetCCType = 2
ElseIf Left(cardNo, 4) = 6011 Then
'Discover
GetCCType = 3
ElseIf Left(cardNo, 2) = 34 Or Left(cardNo, 2) = 37 Then
'American Express
GetCCType = 4
ElseIf (Left(cardNo, 3) >= 300 And Left(cardNo, 3) <= 305) Or _
Left(cardNo, 2) = 36 Or Left(cardNo, 2) = 38 Then
'Diners Club -- I don't know if this works coz i dun haf one to check it wif
GetCCType = 5
Else
GetCCType = 0
End If
End Function
'This is the actual Mod10
Private Function ValidateMod10(ByVal cardNo)
Dim intCounter, intTotal
Dim strTotal
intTotal = 0
For intCounter = 1 To Len(cardNo)
If (intCounter Mod 2) <> 0 Then
strTotal = strTotal & Mid(cardNo, intCounter, 1) * 2
Else
strTotal = strTotal & Mid(cardNo, intCounter, 1)
End If
Next
For intCounter = 1 To Len(strTotal)
intTotal = intTotal + Mid(strTotal, intCounter, 1)
Next
If (intTotal Mod 10) Then
ValidateMod10 = False
Else
ValidateMod10 = True
End If
End Function