Dynamic Credit Card Length

T

Tatakau

I have two objects on a form that I am trying to manipulate. The first is a
combo box to select a credit card type (Mastercard, Visa, Discover, Etc.).
The second is a text field to hold the actual credit card number.

In order to prevent data entry error, I have put an input mask on the text
field of 0000-0000-0000-9999. The last 4 digits are 9s (optional entry)
because one of the credit card types has only 12 digits as opposed to 16.

However, that would mean that someone could enter a 12 digit number for a
card with 16 digits, which is of course not good. Using the above input mask
is a very sloppy fix, and doesn't account for all possible entry errors. Is
there any way to dynamically change the input mask to adjust for which credit
card type is selected?

Visa, Mastercard, etc.: 0000-0000-0000-0000
Custom Card: 0000-0000-0000

Thanks,

Nick
 
K

Klatuu

You can change an input mask using VBA. You could do that in the After
Update event of your card type combo box. A trick you might try, if your
combo row source is a list, is to add a column to the list that is the input
mask for that card:
Mastercard;"0000-0000-0000-0000";Visa;"0000-0000-0000-0000";Custom;"0000-0000-0000"
Make the column width 0 so the user doesn't see it. Then in the After
Update event of the combo:
Me.txtCardNumber.InputMask = Me.cboCardType.Column(1)
 
A

Allen Browne

Input masks are not very useful.

Use the BeforeUpdate event of the *form* to perform the validation on the
Len() of the field.
 
K

Klatuu

Allen,
Your comment on Input Masks suprises me. In most instances they are not
necessary, but for cases like this, phone numbers, or other data that needs
to be in a specific format, I find them useful.
I would consider this situation the perfect place to use an input mask;
otherwise, you would have write code to check the length of the entry against
the length required by the card issuer and ensure it is numeric data. You
would also have to determine whether any - characters are entered and whether
you want to store them in the data base. This very simple input mask
resolves those issues.
 
T

Tatakau

Perfect! I was thinking I'd have to write code like "after update, if
cardtype = Mastercard then inputmask = '0000-0000-0000-0000'"... your
solution is much more clever! Thanks!

Nick
 
A

Allen Browne

In general, input masks have more downsides than positives, such as the
inability to insert a missed digit.

In particular, the number of digits varies beyond the 2 values the OP
suggested, e.g. Visa can be 13, Diner can be 14, AmEx can be 15. Further,
these could change in the future, so hard-coding rules is not a good idea.

A better solution is to create a table of the card types to be handled, and
a related table of the prefix ranges for each card, with the number of
digits required for the prefix range, and whether to use the checksum
algorithm as per:
http://www.scriptarchive.com/ccver.html

We are well outside the capabilities of an input mask here.
 
T

Tatakau

That is definitely something to look out for. I will have to code in an
exception, just in case users run into strange credit cards. I think I
should set the default input mask to Null, and set a exception credit card
type to have an input mask of Null as well. Maybe not the optimal solution,
but since those kinds of exceptions are probably extremely rare, it should
work.

Thanks to both of you!

Nick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top