October 1, 2006
Validate Credit-card Numbers With AJAX
Validate credit card numbers without submitting and refreshing the entire web page.
Entering a credit card number on a web page has become common place. This hack verifies the entered credit card number, then only submits it to the server component if the number is valid. Nothing else changes on the page except for a user message, which notifies the user of any error conditions or that their credit card has passed muster and has been sent to the server to be processed. The server connection would likely be initiated over Secure Sockets Layer (SSL), such as with the HTTPS protocol, and is involved with an e=commerce component that further verifies the purchase information with a merchant bank. This hack, however, just verifies the number, generates a message, and makes an HTTP request using Ajax techniques.
Figure 3-4 shows what the web page looks like.
Enter credit-card number for verification
This is the web page code. It imports two JavaScript files.
XXXredpre#22XXX
The user chooses a credit card type (e.g., "Mastercard"), enters the card number, expiration date, and Card Security Code, and clicks the XXXredpre#198XXX button. However, instead of having the page dissolve and the values depart immediately for the server, the application verifies a few conditions first. The JavaScript makes sure that the fields are not blank or contain a minimum number of characters (such as three for the Card Security Code), then it verifies the card number using the Luhn formula or algorithm.
NOTE
This is a well-known algorithm used to verify ID numbers like credit card numbers.
If one of these checks fails, the hack displays a message in red. Figure 3-5 shows one of these messages.
Time to re-enter the credit card
If the credit card number is verified and everything else has been correctly entered, then the hack uses XXXredpre#199XXX to send this information to a server.
We are not strictly making a secure connection in this hack, but a real application would not send any purchase information unencrypted over a network.
A message in blue notifies the user, as in figure 3-6.
Purchase information passes muster
Verifying the Card Number
cc.js contains the code for responding to the user's button click, as well as for verifying the information and generating a user message. http_request.js creates and calls the methods of XXXredpre#200XXX. See Hack #3. Here is the code for cc.js.
XXXredpre#23XXX
There is a lot of functionality to absorb here, so first we will discuss the button click. When the browser completes loading the web page, this event is captured by the code XXXredpre#201XXX. This event handler is a sensible place to set up other event handlers, because the code is assured that the browser has finished loading other HTML tags that might be used by these handlers. Then the code sets up an event handler for when the user submits the form.
XXXredpre#24XXX
The form's XXXredpre#202XXX event handler points to a function that calls XXXredpre#203XXX, then retuns XXXredpre#204XXX, which effectively cancels the browser's form submission. We are using the request object to send the form values, only after verifying that the submissions are valid. Let's look at the XXXredpre#205XXX function.
XXXredpre#25XXX
This function includes a number of common-sense checks before it validates the credit-card number using another function, XXXredpre#206XXX. If the latter function returns XXXredpre#207XXX, then the code builds a URL for the server component, then uses XXXredpre#208XXX to send the card information.
The XXXredpre#209XXX function is responsible for setting up XXXredpre#210XXX and connecting with the server. The function takes four parameters: The type of request (as in XXXredpre#211XXX or XXXredpre#212XXX), the URL, whether or not the request shuld be asynchronous or not, and the name of the function that will handle the response.
NOTE
This function name should be passed in without the following parentheses. It can also be a function literal, as in XXXredpre#213XXX.
This code appears in the file http_request.js. See Hack #3.
Shooting the Luhn
The XXXredpre#214XXX function verifies that the credit-card number is at least 13 characters and does not contain any letters. If the credit card number passes these checks, then the code removes any spaces or dashes from the string and calls a function that uses the Luhn formula.
XXXredpre#26XXX
Here is the code for the XXXredpre#215XXX function.
XXXredpre#27XXX
Information on the Luhn formula or algorithm is easily found on the web, so we will not take up a lot of space describing it here.
NOTE
This function takes a XXXredpre#216XXX of numbers, applies the formula to the numbers, and returns the sum to XXXredpre#217XXX. If the total can be evenly divided by 10, then the credit card number is valid. Here is the piece of code from XXXredpre#218XXX that makes this determination.
XXXredpre#28XXX
The server component returns a bit of XML indicating success or failure, mimicking the processing of a purchase order, as in XXXredpre#219XXX. The XXXredpre#220XXX function generates a user message from this return value.
XXXredpre#29XXX
The XXXredpre#221XXX function is responsible for generating a styled user message, in red in the event of an error in handling the purchase information, in blue otherwise. Figure 3-6 shows a user message after the credit card has been verified and handled by the server. However, the entire process takes place back stage, the web page never refreshes, and only small parts of the user interface change as the user interacts with the application.
