Learn to Build Credit Card type Detector
In this Video, we built a simple credit card type detector. We try to introduce beginners to:
- XUI Library controls
- Select Case Statements
- If Else Statements
- 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
Great job!
You can use this JavaScript to highlight the code: https://www.b4x.com/android/forum/threads/%F0%9F%90%A2-%F0%9F%91%A9%E2%80%8D%F0%9F%8F%AB-%F0%9F%91%A8%E2%80%8D%F0%9F%8F%AB-highlighting-b4x-code-in-html-pages-using-prism.117448/#post-734584
Thats great will take a look