Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

IsValidCreditCardNumber - Check whether a credit card number is valid
[ All Languages » VB »  String]

Total Hit ( 1742)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Click here to copy the following block
' Validate a credit card numbers
' Returns True if valid, False if invalid
'
' Example:
' If IsValidCreditCardNumber(Value:="1234-123456-12345", IsRequired:=True)

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

  ' If after stripping out non-numerics, there is nothing left,
  ' they entered junk
  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

  'Handle different lengths for different credit card types
  Select Case Mid$(strTemp, 1, 1)
    Case "3"  'Amex
      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"  'Visa
      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"  'Mastercard
      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   'Discover - Dont know rules yet
      If Len(strTemp) > 20 Then
        IsValidCreditCardNumber = False
      End If
  End Select

  'Now check for Check Sum (Mod 10)
  intCheckSum = 0                  
      ' Start with 0 intCheckSum
  blnDoubleFlag = 0                 
      ' Start with a non-doubling
  For i = Len(strTemp) To 1 Step -1          ' Working backwards
    intDigit = Asc(Mid$(strTemp, i, 1))       ' Isolate character
    If intDigit > 47 Then              ' Skip if not a intDigit
      If intDigit < 58 Then
        intDigit = intDigit - 48        ' Remove ASCII bias
        If blnDoubleFlag Then         
            ' If in the "double-add" phase
          intDigit = intDigit + intDigit   '  then double first
          If intDigit > 9 Then
            intDigit = intDigit - 9     ' Cast nines
          End If
        End If
        blnDoubleFlag = Not blnDoubleFlag    ' Flip doubling flag
        intCheckSum = intCheckSum + intDigit  ' Add to running sum
        If intCheckSum > 9 Then         ' Cast tens
          intCheckSum = intCheckSum - 10   ' (same as MOD 10 but
                            ' faster)
        End If
      End If
    End If
  Next

  If intCheckSum <> 0 Then              ' Must sum to zero
    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 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.