9 Replies Latest reply: Nov 14, 2012 10:37 PM by Richard Langlois RSS

    How to format a text field for credit card numbers?

    Richard Langlois

      OK, I found this conversation, http://forums.adobe.com/message/3598185, 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 = [

              /^\d{16}$/

          ];

       

          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!