Skip navigation
Currently Being Moderated

How to format a text field for credit card numbers?

Nov 9, 2012 10:07 PM

Tags: #script #format #text_field #credit_card

OK, I found this conversation,, that gave some useful insight, and I adapted the code presented in it to make sure the user types in a 16-digit number, and it tells them to do it right if they don't.


Here's my problem:

With the code as given, I can either get a single string of 16 digits (i.e.: 1234567890123456), or, I have to make the user manually type in the spaces to get the usual CC format (1234 5678 9012 3456).


What I'm hoping for is some code that will let me allow the user to simply type in 16 uniterrupted digits, but that will then display that entry in the expected format. (So, user types 1234567890123456, but the field once they tab or click out of it will show 1234 5678 9012 3456)


Here's the code I have thus far (like I said, copied alomst verbatim from the link above):

In the Format tab, under Custom Format Script:

function checkFormat(s) {


    if (!s) return true;


    var aRE = [




    return AFExactMatch(aRE, s);




In the Format tab, under Custom Keystroke Script:

(function () {


     if (event.willCommit) {


         var isValid = checkFormat(event.value);


         if (!isValid) {

            app.alert("Please enter a valid 16 digit credit card number with no spaces or dashes.", 1);

            event.rc = false;






I'm sorry it's so simplistic. Though I've rooted around in other programming languages (HTML, C++), so I'm not a complete noob, I don't know anything about Java at all...

Thanks in advance!

  • Currently Being Moderated
    Nov 10, 2012 12:49 AM   in reply to Richard Langlois



    It isn't Java. Acrobat uses Javascript.

    Mark as:
  • Currently Being Moderated
    Nov 10, 2012 1:07 AM   in reply to Bernd Alheit

    I know this isn't at all what you asked, and it may be quite irrelevant to your case or something you are already fully aware of, but I think it's important to say it for the benefit of beginners with Acrobat who might read the thread any time in the future.


    If you are thinking of credit card numbers you may be thinking of a form to be submitted for ordering stuff. It is essential if you do this that the form not be emailed. This breaks the rules of the credit card issuers (so you could lose the card account or worse), and may also break privacy and data security laws in many countries (there may be unlimited fines throughout the EU, for example). The same applies for submitting to an ordinary web site. It is vital to use only submit to an https (secure) web site. The site itself needs to be fully secure, and not email onwards sensitive data. If you aren't already well versed in this, I recommend you consult a programmer experienced in web security to avoid legal and business embarrassements.

    Mark as:
  • Currently Being Moderated
    Nov 10, 2012 1:35 PM   in reply to Richard Langlois

    You need to use a field with the "None" format and you can use the 'arbitrary"mask to create an image for the required format. Beyond that do you want to perform the check digit verification of the entered number?


    Check digits are a number that is one or more digits in the number that can be used to make sure a wrong number has not been entered. Anatomy of Credit Card Number format

    Mark as:
  • Currently Being Moderated
    Nov 12, 2012 11:57 AM   in reply to Richard Langlois


    Mark as:
  • Currently Being Moderated
    Nov 14, 2012 7:32 AM   in reply to Richard Langlois

    Acrobat uses JavaScript and not the Java programing language.


    Many script samples are for different variations of JavaScript and Adobe has its own variation.


    Also if you make spaces or other white space characters part of the value, you need to remove them from the validation.


    A custom validation script could be:


        var cardValid = function(cardNo){   
        var sum = 0, iNum;
        for( var i in cardNo += '' ){
                        iNum = parseInt(cardNo[i]);
                        sum += i%2?iNum:iNum>4?iNum*2%10+1:iNum*2;
        return !(sum%10);
    // process non-blank values
    if(event.value != "") {

    // strip blanks, "-", or "."  from value
    var cCardNumber = event.value.replace(/[- .]/g,"");
    if(cardValid(cCardNumber) == false) {
    app.alert("Invalid Number.\Please re-enter.", 1, 0, "Invalid card Number");
    event.rc =  false;

    } // end not empty value

    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points