Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help Formatting Credit Number in Textbox Control
#1
Hey guys,

I am building an in-house application to store all our customer credit card information in a MySQL database. What I want to do is to take user input of the Credit Card number without any formatting (eg., 1111222233334444) and format it based on the Card Type selected by the user.

TASK #1: FORMAT CREDIT CARD NUMBER
I've tried the following to do the first part, but when I debug the formatted number shows up as ####-####-####-#### instead of the number with dashes.

Code:
TextBoxCard.Text = Format(TextBoxCard.Text, "#### #### #### ####")

TASK #2: DETECT CARD TYPE
Based on the card number entered, I want the application to auto-select the card type from a read-only combobox. I'm not sure how to go about this. The different types of credit card number formats are listed here: http://www.wysiwyg.co.il/Anatomy-of-Cred...ormats.asp

Your help would be most appreciated, thanks.
//Kismet
#2
Well, it took me some time to figure this one out, as I was having the same problems as you, but apparently the correct "Format String", is:

{0:0000 0000 0000 0000}

Interesting..

Anyway, I have created a class that will do the formatting thing for you! It's pretty easy to understand!
[code2=vbnet]Public Class CardFormatter
Private cardDictionary As Dictionary(Of String, String)

Public Sub New()
'Now, we need to create the dictionary
cardDictionary = New Dictionary(Of String, String)

'Add the contents
cardDictionary.Add("American Express", "{0:0000 000000 00000}")
cardDictionary.Add("Visa", "{0:0000 0000 0000 0000}")
cardDictionary.Add("Mastercard", "{0:0000 0000 0000 0000}")
cardDictionary.Add("Discover", "{0:0000 0000 0000 0000}")
cardDictionary.Add("Isracard", "{0:0000 0000}")
cardDictionary.Add("Diners Club", "{0:0000 000000 0000}")
End Sub

Public Function GetCardNames() As List(Of String)
Return New List(Of String)(cardDictionary.Keys)
End Function

Public Function GetFormattedString(ByVal cardName As String, ByVal cardNumber As String) As String
Dim formatString As String = cardDictionary.Item(cardName)
Return String.Format(formatString, Long.Parse(cardNumber))
End Function
End Class[/code2]

First, put something like this at the top of your form class code:
[code2=vbnet]Private CF As New CardFormatter[/code2]

Now all you need to do is add a combobox and textbox to your form, and when the form loads:
[code2=vbnet]ComboBox1.Items.AddRange(CF.GetCardNames.ToArray)[/code2]

Then, when the user submits the text box:
[code2=vbnet]MsgBox("Credit Card Number: " & CF.GetFormattedString(ComboBox1.Text, TextBox1.Text))[/code2]
My Blog | My Setup | My Videos | Have a wonderful day.
#3
Brandon,

Thanks for your assistance. I'm about to try out the code. I can see how the combobox will be populate by the dictionary, but I don't see how this code will format the text in the card number textbox.

Am I missing something?
//Kismet
#4
Well, you simply insert the card number into a textbox (We'll call it textbox1), and the formatted version of the card number can be retrieved with:
[code2=vbnet]CF.GetFormattedString(ComboBox1.Text, TextBox1.Text))[/code2]
My Blog | My Setup | My Videos | Have a wonderful day.
#5
Okay, so it didn't work as I thought it would. Here's what I did per your instructions:

I created a class with the following code:
Code:
Public Class CardFormatter

    Private cardDictionary As Dictionary(Of String, String)

    Public Sub New()
        'Now, we need to create the dictionary
        cardDictionary = New Dictionary(Of String, String)

        'Add the contents
        cardDictionary.Add("Amex", "{0:0000 000000 00000}")
        cardDictionary.Add("Visa", "{0:0000 0000 0000 0000}")
        cardDictionary.Add("Mastercard", "{0:0000 0000 0000 0000}")
        cardDictionary.Add("Discover", "{0:0000 0000 0000 0000}")
        cardDictionary.Add("Isracard", "{0:0000 0000}")
        cardDictionary.Add("Diners", "{0:0000 000000 0000}")
    End Sub

    Public Function GetCardNames() As List(Of String)
        Return New List(Of String)(cardDictionary.Keys)
    End Function

    Public Function GetFormattedString(ByVal cardName As String, ByVal cardNumber As String) As String
        Dim formatString As String = cardDictionary.Item(cardName)
        Return String.Format(formatString, Long.Parse(cardNumber))
    End Function

End Class

I added the following line to the top of my form:
Code:
Private CF As New CardFormatter

I added the following line to the LostFocus event of my textbox (TextBoxCard):
Code:
CF.GetFormattedString(ComboBoxCardType.Text, TextBoxCard.Text)

PROBLEMS:

I noticed that TextBoxCard (which contains the credit card number) doesn't format as intended once the user tabs out of the field.

Instead VB throws a KeyNotFound exception that says:
Quote:The given key was not found in the dictionary
and then points to the following line in the CardFormatter class:
Code:
Dim formatString As String = cardDictionary.Item(cardName)

I found that when I select the Card Type first and then go back to enter the credit card number, everything works fine - except for the formatting. This doesn't work because what I'm intending is to prevent the user from selecting the Card Type. I want the application to choose the Card Type based on the format of the Credit Card Number entered.

Please pardon me, if it seems like I'm asking too much. But I'm learning in the process and I hope you can understand?
//Kismet
#6
Well, automatically detecting the card type based on the numbers that are typed in is going to be difficult seeing that multiple card types have the same number scheme (i.e: Visa and Mastercard)

Anyway, in order to correctly format the textbox, you're going to use the code:
[code2=vbnet]TextBoxCard.Text = CF.GetFormattedString(ComboBoxCardType.Text, TextBoxCard.Text)[/code2]

Instead of:
[code2=vbnet]CF.GetFormattedString(ComboBoxCardType.Text, TextBoxCard.Text)[/code2]
My Blog | My Setup | My Videos | Have a wonderful day.
#7
Brandon,

All seems to be working fine. One problem though, I noticed that if the user made a mistake and tried to re-enter the textbox to correct the card number and exception is thrown - that the input format is wrong.

I traced the error back to the fact that since we already formatted to ####-####-####-####, the code is now taking that and trying to process it.

So I tried to resolve this by using the following code (both on the MouseClick and GotFocus events of TextBoxCard) to replace the dashes - but it didn't work.

Code:
TextBoxCard.Text.Replace("-", "")

Am I doing it wrong?
//Kismet
#8
kismetgerald Wrote:[code2=vbnet]TextBoxCard.Text.Replace("-", "")[/code2]

Am I doing it wrong?

Well, you would actually want to use the code:
[code2=vbnet]TextBoxCard.Text = TextBoxCard.Text.Replace("-", "")[/code2]

However, you would probably want a better way to do this. Since an error is thrown and you want to avoid it, take a look at using Try in order to catch the error!
My Blog | My Setup | My Videos | Have a wonderful day.
#9
Thanks Brandon.
//Kismet
#10
No problem, glad that I can help!
My Blog | My Setup | My Videos | Have a wonderful day.
#11
Brandon,

VB is still throwing an exception about input format being wrong - but this only happens after I enter the credit card number wrong and to back into the textbox to fix the number. The exception is thrown after I tab out of the textbox.

What now?
//Kismet
#12
Do a little something like this:
[code2=vbnet]Private Sub TextBoxCard_LostFocus(sender As Object, e As System.EventArgs) Handles TextBoxCard.LostFocus
Try
TextBoxCard.Text = CF.GetFormattedString(ComboBoxCardType.Text, TextBoxCard.Text)
Catch ex As Exception
'If the code has reached here, an error has occured. We'll just ignore it.
End Try
End Sub[/code2]
My Blog | My Setup | My Videos | Have a wonderful day.


Possibly Related Threads…
Thread Author Replies Views Last Post
  Custom tab control help and making controls Derek275 4 21,251 11-16-2012, 09:51 AM
Last Post: Snake_eyes
  Winsock Control Covert2String 7 25,368 11-27-2011, 08:57 PM
Last Post: brandonio21
  Custom Tab Control xolara 5 20,466 04-24-2011, 07:12 PM
Last Post: xolara

Forum Jump:


Users browsing this thread: 1 Guest(s)