# Friday, December 02, 2005
This VB.Net code checks the validity of a credit card number. Pass a credit card number to the function and it returns true or false based upon the CC number's validity. This function is declared as Shared so it can be called without having to create a object of the containing class.
  Public Shared Function ValidateCCNumber(ByVal sCCNumber As String) As Boolean

    Dim i, w, y As Integer
    Dim x As String
    y = 0

    sCCNumber = sCCNumber.Trim()

    w = 2 * (Len(sCCNumber) Mod 2)

    For i = Len(sCCNumber) - 1 To 1 Step -1
      x = Mid(sCCNumber, i, 1)

      If IsNumeric(x) Then
        Select Case (i Mod 2) + w
          Case 0, 3
            y = y + CInt(x)
          Case 1, 2
            x = CInt(x) * 2
            If x > 9 Then
              y = y + (x \ 10) + (x - 10)
            Else
              y = y + x
            End If
        End Select
      End If

    Next
    y = 10 - (y Mod 10)
    If y > 9 Then y = 0

    Return (CStr(y) = Right(sCCNumber, 1))

  End Function

If you found this post helpful, please "Kick" it so others can find it too:

kick it on DotNetKicks.com
.NET | ASP.NET | VB.NET