Determine Credit Card Type - Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

Determine Credit Card Type

 

For this tutorial, i'll show you how to create a program that determine the credit card type by entering the numbers of a credit card:

For this tutorial you need 2 TextBoxes and 1 Button
First we make sure that user enter numbers only in TextBox1:

   Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If Asc(e.KeyChar) <> 13 AndAlso Asc(e.KeyChar) <> 8 _
        AndAlso Not IsNumeric(e.KeyChar) Then
            MessageBox.Show("Please Enter Numbers Only")
            e.Handled = True
        End If
    End Sub

TextBox1.text will be used to enter the credit card numbers
'TextBox2.text will show the results:

'Determine if the card is VISA:
Public Sub visa()
        TextBox1.SelectionStart = (0)
        TextBox1.SelectionLength = (1)
        If TextBox1.SelectedText = 4 And TextBox1.Text.Length = 16 Then
            TextBox2.Text = " Visa Card"
        Else
            'If card is not a visa we check if it is a mastercard
            mastercard()
        End If
End Sub

Determine if the card is MasterCard:
 Public Sub mastercard()
        TextBox1.SelectionStart = (0)
        TextBox1.SelectionLength = (2)
        If TextBox1.SelectedText = 51 And TextBox1.Text.Length = 16 Then
            TextBox2.Text = " MasterCard"
        ElseIf TextBox1.SelectedText = 52 And TextBox1.Text.Length = 16 Then
            TextBox2.Text = " MasterCard"
        ElseIf TextBox1.SelectedText = 53 And TextBox1.Text.Length = 16 Then
            TextBox2.Text = " MasterCard"
        ElseIf TextBox1.SelectedText = 54 And TextBox1.Text.Length = 16 Then
            TextBox2.Text = " MasterCard"
        ElseIf TextBox1.SelectedText = 55 And TextBox1.Text.Length = 16 Then
            TextBox2.Text = " MasterCard"
        Else
            'if card is not a MasterCard we Check if it is American Express
            americanexpress()
        End If
    End Sub

Determine if the card is American Express

 Public Sub americanexpress()
        TextBox1.SelectionStart = (0)
        TextBox1.SelectionLength = (2)
        If TextBox1.SelectedText = 34 And TextBox1.Text.Length = 15 Then
            TextBox2.Text = "American Express"
        ElseIf TextBox1.SelectedText = 35 And TextBox1.Text.Length = 15 Then
            TextBox2.Text = "American Express"
        ElseIf TextBox1.SelectedText = 36 And TextBox1.Text.Length = 15 Then
            TextBox2.Text = "American Express"
        ElseIf TextBox1.SelectedText = 37 And TextBox1.Text.Length = 15 Then
            TextBox2.Text = " American Express"
        Else
            diners1()
        End If
    End Sub
    


There are two options for Diners Cards: NOTE: YOU HAVE TO ENTER BOTH OPTIONS
'First:
Public Sub diners1()
        TextBox1.SelectionStart = (0)
        TextBox1.SelectionLength = (3)
        If TextBox1.SelectedText = 300 And TextBox1.Text.Length = 14 Then
            TextBox2.Text = " Diners Club"
        ElseIf TextBox1.SelectedText = 301 And TextBox1.Text.Length = 14 Then
            TextBox2.Text = " Diners Club"
        ElseIf TextBox1.SelectedText = 302 And TextBox1.Text.Length = 14 Then
            TextBox2.Text = " Diners Club"
        ElseIf TextBox1.SelectedText = 303 And TextBox1.Text.Length = 14 Then
            TextBox2.Text = " Diners Club"
        ElseIf TextBox1.SelectedText = 304 And TextBox1.Text.Length = 14 Then
            TextBox2.Text = " Diners Club"
        ElseIf TextBox1.SelectedText = 305 And TextBox1.Text.Length = 14 Then
            TextBox2.Text = " Diners Club"
        Else
            diners2()
        End If
End Sub


Second Option


Public Sub diners2()
        TextBox1.SelectionStart = (0)
        TextBox1.SelectionLength = (2)
        If TextBox1.SelectedText = 36 And TextBox1.Text.Length = 14 Then
            TextBox2.Text = " Diners Club"
        ElseIf TextBox1.SelectedText = 38 And TextBox1.Text.Length = 14 Then
            TextBox2.Text = " Diners Club"

        Else
            discover1()
        End If

End Sub



Determine if card is DISCOVER

Public Sub discover1()
        TextBox1.SelectionStart = (0)
        TextBox1.SelectionLength = (4)
        If TextBox1.SelectedText = 6011 And TextBox1.Text.Length = 16 Then
            TextBox2.Text = " Discover Card"
        Else
            discover2()
        End If
    End Sub
    Public Sub discover2()
        TextBox1.SelectionStart = (0)
        TextBox1.SelectionLength = (2)
        If TextBox1.SelectedText = 65 And TextBox1.Text.Length = 16 Then
            TextBox2.Text = " Discover Card"
        Else
            TextBox2.Text = "Unknown Card"
        End If
    End Sub

Add the following code to the button click event to get the result:

visa()

Enjoy!!


Share This

Home | About | Contact | Privacy Policy

Copyright visual-basic-tutorials.com 2017 - All Rights Reserved.