Help Formatting Credit Number in Textbox Control - Printable Version +- BP Forums (https://bpforums.info) +-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55) +--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56) +---- Forum: VB.NET (Visual Basic 2010/2008) (https://bpforums.info/forumdisplay.php?fid=8) +---- Thread: Help Formatting Credit Number in Textbox Control (/showthread.php?tid=612) |
Help Formatting Credit Number in Textbox Control - kismetgerald - 08-24-2012 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-CreditCard-Number-formats.asp Your help would be most appreciated, thanks. Re: Help Formatting Credit Number in Textbox Control - brandonio21 - 08-24-2012 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] Re: Help Formatting Credit Number in Textbox Control - kismetgerald - 08-25-2012 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? Re: Help Formatting Credit Number in Textbox Control - brandonio21 - 08-25-2012 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] Re: Help Formatting Credit Number in Textbox Control - kismetgerald - 08-26-2012 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 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 dictionaryand 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? Re: Help Formatting Credit Number in Textbox Control - brandonio21 - 08-26-2012 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] Re: Help Formatting Credit Number in Textbox Control - kismetgerald - 08-27-2012 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? Re: Help Formatting Credit Number in Textbox Control - brandonio21 - 08-27-2012 kismetgerald Wrote:[code2=vbnet]TextBoxCard.Text.Replace("-", "")[/code2] 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! Re: Help Formatting Credit Number in Textbox Control - kismetgerald - 08-29-2012 Thanks Brandon. Re: Help Formatting Credit Number in Textbox Control - brandonio21 - 08-30-2012 No problem, glad that I can help! Re: Help Formatting Credit Number in Textbox Control - kismetgerald - 08-30-2012 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? Re: Help Formatting Credit Number in Textbox Control - brandonio21 - 08-30-2012 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] |