Visual Basic Code of the Week (COTW)
Issue #118
Online Version at (paid subscribers only)
All content and source code is Copyright (c) 2000 by C&D Programming Corp. No part of this issue can be reprinted or distributed in any manner without express written permission of C&D Programming Corp.

Issue topic: Formatting credit card numbers (non-secure and secure)

Earn REAL money surfing the web!

If you would like to make some extra cash for surfing the web, jump to


In this Issue

In this issue we discuss how properly (at least in our opinion) format credit card numbers. This routine is useful for anyplace a credit card number is captured or displayed. It should work well in VBScript with very little modifications.

If you have any questions about using this module, let us know at


This module contains a single routine called FormatCreditCardNumber that takes a string of digits and processes it to produce a nicely formatted credit card number.


Public Function FormatCreditCardNumber(sCardNumber As String, Optional bSecure As Boolean = False) As String

This function returns a formatted credit card number using separate formats for the different types of credit/charge cards. An American Express card is 15 digits and has the number grouped differently than Visa, MasterCard and Novus cards. The default number separator is a - (dash). If you want the return string to show a secure representation of the card number just set the bSecure option to True. This will cause this function to put X characters in the place of all numbers except the last four digits.

American Express numbers will be formatted as 1234-123456-12345. All others will be formatted as 1234-1234-1234-1234.

One really nice feature of this routine is that the input string specified by sCardNumber can be any format as long as it has 15 or 16 digits. For example, 1111-1111-2222-2222 or 11111111 222 2 2222 would result in the same output string. The routine "cleans" the string first by removing any non-numeric character and then reformats it according to the specific rules defined in this function. This is REALLY useful for those web sites that gather information from users that can enter numbers any way they wish.

Sample Usage

Some examples are shown using different credit card numbers (NOTE: the credit card numbers are not real card numbers).

    Debug.Print FormatCreditCardNumber("4234123412341234")
    ' will print out as 4234-1234-1234-1234
    Debug.Print FormatCreditCardNumber("4234123412341234",True)
    ' will print out as XXXX-XXXX-XXXX-1234
    Debug.Print FormatCreditCardNumber("323412341234123",True)
    ' will print out as XXXX-XXXXXX-X4123

Source Code

To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address:

This document is available on the web

Paid subscribers can view this issue in HTML format. There is no additional source or information in the HTML formatted document. It just looks a little better since we have included some HTML formatting. Just point your browser to link at the top of this document.

Get paid to surf the web!

If you would like to get paid for surfing the web, jump to

Other links

Contact Information

C&D Programming Corp.
PO Box 20128
Floral Park, NY 11002-0128
Phone or Fax: (212) 504-7945