B4X Programming

B4X Programming – Learn to Build Credit card detector

Learn to Build Credit Card type Detector

In this Video, we built a simple credit card type detector. We try to introduce beginners to:

  1. XUI Library controls
  2. Select Case Statements
  3. If Else Statements
  4. Comparison and logical operators and many more.

Take a look at the code below

Sub Globals

    Private btnCheckCard As B4XView
    Private edtCardNum As B4XView
    Private imgCardLogo As B4XView

    Dim amXpress, dinersClub, discover,mastercard,visa, nocard As B4XBitmap
    Dim h As Int
    
    Dim oneDigit, twoDigit,fourDigit As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("ui")
    
    h = imgCardLogo.Width 'We will use this to set both width and height since they are equal
    
    amXpress = LoadBitmapResize(File.DirAssets,"american-express.png",h,h,True)
    dinersClub = LoadBitmapResize(File.DirAssets,"dinners-club.png",h,h,True)
    discover = LoadBitmapResize(File.DirAssets,"discover.png",h,h,True)
    mastercard = LoadBitmapResize(File.DirAssets,"mastercard.png",h,h,True)
    visa = LoadBitmapResize (File.DirAssets,"visa.png",h,h,True)
    nocard = LoadBitmapResize (File.DirAssets,"nocard.png",h,h,True)

End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub btnCheckCard_Click
    'Lets validate
    If edtCardNum.Text = "" Or edtCardNum.Text.Trim = "" Then
        ToastMessageShow("Please enter the card Number",True)
        Return
    End If
    
    Dim cardNum As String = edtCardNum.text
    Dim cardNumberLength As Int = cardNum.Length
    
    'Extract the digits from the card number
    oneDigit = cardNum.SubString2(0,1)
    twoDigit = cardNum.SubString2(0,2)
    'threeDigit = cardNum.SubString2(0,3)
    fourDigit = cardNum.SubString2(0,4)
    
    'First layer: Lets use the card number to determine the card length
    Select cardNumberLength
        Case 14 'Check for Diners Club
    
        'Detect Dinners club TYPE 1
        If twoDigit = 36 Or twoDigit = 38 Then
            imgCardLogo.SetBitmap(dinersClub)
        End If
    
        'Detect Dinners Club TYPE 2 (USING 4 digits)
        If fourDigit >= 301 And fourDigit <=305 Then
            imgCardLogo.SetBitmap(dinersClub)
        End If
    
    Case 15 'Check for American Express
    
        'Detect American Express card Type 1 35,36,37,54
        If twoDigit = 35 Or _
            twoDigit = 36 Or _
            twoDigit = 37 Or _
            twoDigit = 54 Then
            imgCardLogo.SetBitmap(amXpress)
        End If
    
    Case 16 'Cehck for Visa and Mastercard and Discover
    
        'Detect Visa with one digit starting with 4
        If oneDigit = 4 Then
            imgCardLogo.SetBitmap(visa)
        else if twoDigit = 65 Then
            imgCardLogo.SetBitmap(discover)
        else if fourDigit = 6011 Then
            imgCardLogo.SetBitmap(discover)
        else if twoDigit >= 51 And twoDigit <= 54 Then
            imgCardLogo.SetBitmap(mastercard)
        End If

    Case Else
        imgCardLogo.SetBitmap(nocard)
        ToastMessageShow("Sorry, invalid card number",True)
    End Select

End Sub 

2 Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our Youtube Channel

Be informed when we upload new videos and source codes