|
|
|
Click here to copy the following block |
Function IsValidCreditCardNumber(Value As Variant, Optional ByVal IsRequired As _ Boolean = True) As Boolean Dim strTemp As String Dim intCheckSum As Integer Dim blnDoubleFlag As Boolean Dim intDigit As Integer Dim i As Integer
On Error GoTo ErrorHandler
IsValidCreditCardNumber = True Value = Trim$(Value)
If IsRequired And Len(Value) = 0 Then IsValidCreditCardNumber = False End If
For i = 1 To Len(Value) If IsNumeric(Mid$(Value, i, 1)) Then strTemp = strTemp & Mid$(Value, i, _ 1) Next If IsRequired And Len(strTemp) = 0 Then IsValidCreditCardNumber = False End If
Select Case Mid$(strTemp, 1, 1) Case "3" If Len(strTemp) <> 15 Then IsValidCreditCardNumber = False Else Value = Mid$(strTemp, 1, 4) & "-" & Mid$(strTemp, 5, _ 6) & "-" & Mid$(strTemp, 11, 5) End If Case "4" If Len(strTemp) <> 16 Then IsValidCreditCardNumber = False Else Value = Mid$(strTemp, 1, 4) & "-" & Mid$(strTemp, 5, _ 4) & "-" & Mid$(strTemp, 9, 4) & "-" & Mid$(strTemp, 13, 4) End If Case "5" If Len(strTemp) <> 16 Then IsValidCreditCardNumber = False Else Value = Mid$(strTemp, 1, 4) & "-" & Mid$(strTemp, 5, _ 4) & "-" & Mid$(strTemp, 9, 4) & "-" & Mid$(strTemp, 13, 4) End If Case Else If Len(strTemp) > 20 Then IsValidCreditCardNumber = False End If End Select
intCheckSum = 0 blnDoubleFlag = 0 For i = Len(strTemp) To 1 Step -1 intDigit = Asc(Mid$(strTemp, i, 1)) If intDigit > 47 Then If intDigit < 58 Then intDigit = intDigit - 48 If blnDoubleFlag Then intDigit = intDigit + intDigit If intDigit > 9 Then intDigit = intDigit - 9 End If End If blnDoubleFlag = Not blnDoubleFlag intCheckSum = intCheckSum + intDigit If intCheckSum > 9 Then intCheckSum = intCheckSum - 10 End If End If End If Next
If intCheckSum <> 0 Then IsValidCreditCardNumber = False End If
ExitMe: Exit Function ErrorHandler: Err.Raise Err.Number, "IsValidCreditCardNumber", Err.Description
End Function |
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|