Credit Card Validation and Verification

This chapter goes through credit card validation and verification, such as is vital during the checkout procedure of an online shopping application. The chapter as a whole covers all aspects of the checkout procedure in detail.

This sample is taken from Chapter 7: "Credit Card Validation and Verification" of the Glasshaus title "Usable Shopping Carts"

Asp/SQL Server

Unlike PHP we do not have to worry about abandoning the Session object because we have moved the user over to the secure server. The generalized format for this page (OnlineForm.asp) is similar to the address screen. There are two primary procedures ValidateForm() and BuildForm(). In addition there is a function CheckCCNumLuhn() that is used as a simple credit card number validator; we will cover this later in the chapter.

The base code for the page is a simple statement that determines the page state and directs to the appropriate subprocedure:

<%

  If lcase(Request.Form("submit")) = "submit" Then

      ValidateForm() 

  Else 

    BuildForm()

  End IF

%>

We will begin with the ValidateForm() subprocedure:

Sub ValidateForm()

  Dim boolFormIsValid 'as Boolean

  boolFormIsValid = true

  If Len(Request.Form("ccname")) < 1 Then boolFormIsValid = False

  If Len(Request.Form("cctype")) < 1 Then boolFormIsValid = False

  If Cint(Request.Form("ccmonth")) < 1 Then boolFormIsValid = False

  If Cint(Request.Form("ccyear")) < 1 Then boolFormIsValid = False

  If Len(Request.Form("ccnumber")) < 1 Then boolFormIsValid = False

  If Len(Request.Form("cczip")) < 1 Then boolFormIsValid = False

  If Not CheckCCNumLuhn(Request.Form("ccnumber")) Then boolFormIsValid = False

  If Not boolFormIsValid Then

    BuildForm()

  Else

    'This is where you pass the information to Merchant account interface

    Response.write("Information submitted to merchant account.")

  End If

End Sub

Unlike the address page, we do not have any regular expressions to validate user input. For almost all of the inputs, we simply verify that the user entered some data. The one exception is the credit card number. An additional function to perform a Luhn check on the credit card number has been created and the card number is passed to that. The information on the Luhn formula will be discussed further in this chapter. If the form is valid, this would be the point at which the information should be passed over to the merchant account interface to be run against the entered credit card. In the event that the form input is invalid, then the BuildForm() procedure is called and any errors are noted for the user to correct.

The BuildForm() procedure follows the same generalized format as the one in the address page. The code is written to output the form, and in the case of submission, to display the error information necessary to alert the user to problem data in the form:

BuildForm()

  If Request.Form("Submit") = "Submit" Then

    blnShowErrors = true

  Else

    blnShowErrors = false

  End IF

  %>

The procedure begins by detecting whether it was called with a submit event or not. In the event that the form was submitted back to the page, the flag blnShowErrors is set to true:

    <h1>Credit Card Information</h1>

        <p>All Fields Are Required.</p>

    <form name="CCForm" action="OnlineForm.asp" method=POST>

    <p align=center><b>Your name as it appears on the card</b><br>

    <input type="text" name="ccname" value="<%=Request.Form("ccname")%>" size=60>

    <%if blnShowErrors AND Len(Request.Form("ccname")) < 1 Then response.write("<span class=""error"">Please Enter Your Name</span>")%>

    </p><hr>

    <p align=center><b>Type of Card</b><br>

Initially we establish the page and form specific information. In addition, we have the validation information for the form's first field.

    <table width=50% cellspacing=2 cellpadding=0>

    <tr><td align=right>Visa</td><td><input type="radio" name="cctype" value="visa" <%if request.form("cctype")="visa" then response.write("checked")%>><br></td></tr>

    <tr><td align=right>MasterCard</td><td><input type="radio" name="cctype" value="mc" <%if request.form("cctype")="mc" then response.write("checked")%>><br></td></tr>

    <tr><td align=right>American Express</td><td><input type="radio" name="cctype" value="amex"  <%if request.form("cctype")="amex" then response.write("checked")%>><br></td></tr>

    </table></p><hr>

Next we handle the credit card type. This segment of the form is a collection of radio buttons. The primary item to  remember is that radio buttons have a "checked" status, so when outputting the form, we need to make sure that the validation sets this status correctly.

    <p align=center><b>Expiration Date</b></p>

    <table width=100% cellspacing=5 cellpadding=0>

      <tr><td align=center>Month</td><td align=center>Year</td></tr>

      <tr>

        <td align=center>

        <select name="ccmonth">

          <option value=0>[Choose Month]

  <%

  Dim arrMonths

  arrMonths = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")

  For i = 0 to UBound(arrMonths)

    Response.Write("<option value=" & (i+1))

    If Cint(Request.Form("ccmonth")) = (i+1) then response.write(" selected ")

    Response.write(">" & arrMonths(i) & vbcrlf)

  Next

  %> 

        </select>

        <%if blnShowErrors AND Cint(Request.Form("ccmonth")) < 1 Then response.write("<span class=""error"">Please Select the expiration month</span>")%>

        </td>

The expiration information for a credit card is critical. We need to make sure that this is captured correctly. The first field is expiration month. There are a number of different methods for outputting this information. It may either be stored within our database or, in this example, an array at page level. To output the options for the select the month array is looped across similar to how we would loop across a recordset. In addition, there is a check to set the correct selected value in the event of a submit.

        <td align=center><select name="ccyear">

          <option value=0>[Choose Year]

  <%

  Dim thisYear

  thisYear = Year(Now)

  For i = 0 to 4

    Response.Write("<option value=" & (thisYear+i))

    If Cint(Request.Form("ccyear")) = (thisYear + i) Then Response.write(" selected ")

    Response.Write(">" & (thisYear+i))

  Next

  %> 

          </select>

          <%if blnShowErrors AND Cint(Request.Form("ccyear")) < 1 Then response.write("<span class=""error"">Please Select the expiration year</span>")%>

        </td></tr>

The year is handled in a similar fashion to the month. Since years, unlike months, are an indirect starting point, the code grabs the current year and then builds the select based on adding five years to the current year. Again, we have inserted the validation information into the loop to set the proper value for the select.

    <tr><td colspan=2><hr></td></tr>

      <tr><td align=center>Card Number</td>

        <td align=center>Zip Code where you recieve your statement</td>

      </tr><tr>

        <td align=center>

        <input type="text" name="ccnumber" value="<%=request.form("ccnumber")%>">

        <%If blnShowErrors AND len(request.form("ccnumber"))<1 AND NOT CheckCCNumLuhn(Request.Form("ccnumber"))Then

        response.write("<span class=""error"">Please verify your credit card number</span>")

        End If %>

        </td>

        <td align=center>

        <input type="text" name="cczip" value="<%=request.form("cczip")%>">

        <%if blnShowErrors AND Len(Request.Form("cczip")) < 1 Then response.write("<span class=""error"">Please enter your zip code</span>")%>

        </td>

      </tr><tr><td colspan=2><hr></td></tr>

      <tr><td align=center><input type="submit" name="submit" value="Submit"></td>

        <td align=center><input type="reset" name="reset" value="Reset"></td></tr>

    </table></form>

Finally the form takes the input of the credit card number and the user's zip code. You'll note that we invoke the Luhn function again in this section when determining whether or not to output an error message for the credit card information. Finally the form is terminated with the appropriate submit button functionality.

George Petrov

George PetrovGeorge Petrov is a renowned software writer and developer whose extensive skills brought numerous extensions, articles and knowledge to the DMXzone- the online community for professional Adobe Dreamweaver users. The most popular for its over high-quality Dreamweaver extensions and templates.

George is also the founder of Wappler.io - the most Advanced Web & App Builder

See All Postings From George Petrov >>

Comments

Be the first to write a comment

You must me logged in to write a comment.