Format a credit card number

# Thread: Format a credit card number

1. Senior Member
Join Date
Dec 1969
Posts
1,156

## Format a credit card number

I&#039;ve be searching for some sample code / advice on formatting a credit card number (split into groups of 4 digits) using classic ASP.<BR><BR>I can&#039;t find any at all! Can anyone point me in the right direction?<BR><BR>Thanks!

2. Senior Member
Join Date
Dec 1969
Posts
6,476

## RE: Format a credit card number

Make sure the number is a string<BR><BR>Then you can use the Right() Mid() functions to get the parts you want and build another string with it.<BR>

3. Senior Member
Join Date
Dec 1969
Posts
1,316

## RE: Format a credit card number

I have a pretty solid implementation of this in vb.net<BR><BR>If you would like it, ill post the whole thing.

4. Senior Member
Join Date
Dec 1969
Posts
6,476

## RE: Format a credit card number

&#060;%<BR>strNumber = "1234567823456789"<BR>strNumber = CStr(strnumber) &#039;make sure it is a string<BR><BR>IF Len(strNumber) = 16 Then &#039;make sure we have exactly 16 numbers <BR> strNewNumber = ""<BR> strNewNumber = strNewNumber & Mid(strNumber, 1, 4) & "-"<BR> strNewNumber = strNewNumber & Mid(strNumber, 5, 4) & "-"<BR> strNewNumber = strNewNumber & Mid(strNumber, 9, 4) & "-"<BR> strNewNumber = strNewNumber & Mid(strNumber, 13, 4)<BR> Response.Write "&#060;p&#062;" & strNewNumber <BR>Else<BR> Response.Write "We dont have 16 numbers to work with"<BR>End IF<BR>%&#062;

5. Senior Member
Join Date
Dec 1969
Posts
6,476

## RE: Format a credit card number

Sorry didnt see ya post this.

6. Senior Member
Join Date
Dec 1969
Posts
1,156

## Thanks!

&nbsp;<BR>I&#039;m sure I can take it from there to cover what happens if there are more than 16 numbers (eg. Debit cards).<BR><BR>Thanks for your speedy responses.<BR><BR>

7. Senior Member
Join Date
Dec 1969
Posts
6,476

## RE: Thanks!

make sure they are all valid numbers as well...<BR>ie<BR>12h4127472637459<BR><BR>Probably a regex would be best used?<BR>Or the isNumeric() function I guess...<BR><BR>Just saying there is still lots to consider, other than what i posted....<BR>

8. Senior Member
Join Date
Dec 1969
Posts
1,316

## Theres alot more though

Did you have the intention to also do things like:<BR>Extract the Credit Card Type based on the Number<BR>Validate that the number entered is a valid number of a specific type?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•