spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / carts / chap7 / 3 To page 1To page 2current pageTo page 4
[previous] [next]

Usable Shopping Carts, Chapter 7: CC Validation and Verification

Vice President of Risk Technology - READY TO HIRE! (NYC)
Next Step Systems
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume
Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs


PHP

One of the "requirement" strings in our validation script (validate.inc) is "ccnumber" (which we've added to the original PHP validation code we created in Usable Forms for the Web). This means that we need to look for this string as a substring of each field name in the form we're validating, and if we've found it, a variable named $ccnumber has been set to TRUE. If it is, then we know we need to try to validate the current field as credit card information.

      if($ccnumber)

      {

First, we get rid of any spaces or dashes the user might have typed in, then test the result to see if it's a numeric value ($not_int is the regular expression /\D/, which matches any non-digit characters in a string):

        $value = preg_replace("[ -]+","",$value);

        if( $value!="" && preg_match($not_int, $value ) )

        {

If we find any non-digits in the value, we add an appropriate error message onto the value of the variable $err_msg, and set our validation flag $validated to FALSE:

          $err_msg .= "The <b>$field_name</b> should contain only 

                             the digits 0-9.<br />\n";

          $validated = FALSE;

        }

        else

        {

Otherwise, we check to see if the user has selected a credit card type:

          if( isset($HTTP_POST_VARS["Credit0Card0Type_required"]) )

          {

If so, we get that type as the variable $cc_type, the value in the Card Number field as $cc_number, and the length of that value as $cc_length:

            $cc_type = $HTTP_POST_VARS["Credit0Card0Type_required"];

            $cc_number = 

                    $HTTP_POST_VARS["Credit0Card0Number_ccnumber_required"];

            $cc_length = strlen($cc_number);

Now we test the length of the number against what's correct for its type, setting $validated to TRUE or FALSE accordingly:

            switch($cc_type)

            {

              case "MasterCard":

                $validated = $cc_length == 16;

                break;

              case "Visa":

                $validated = ($cc_length == 13 || $cc_length == 16);

                break;

              case "American Express":

                $validated = $cc_length == 15;

                break;

            }

If $validated is FALSE, the number has the wrong number of digits for the type of card indicated:

            if(!$validated)

              $err_msg .= "Wrong number of digits for $cc_type.<br >\n";

            else

            {

Otherwise, we check to see if the number has the correct prefix for that type:

              switch($cc_type)

              {

                case "MasterCard":

                  $prefix = substr($cc_number, 0, 2);

                  $validated = $validated && ($prefix > 50 && $prefix < 56);

                  break;

                case "Visa":

                  $prefix = substr($cc_number, 0, 1);

                  $validated = $validated && ($prefix == 4);

                  break;

                case "American Express":

                  $prefix = substr($cc_number, 0, 2);

                  $validated = $validated && ($prefix == 34 || $prefix == 37);

                  break;

              }

              if(!$validated)

                $err_msg .= "Invalid prefix for $cc_type number.<br />\n";

              else

              {

home / programming / carts / chap7 / 3 To page 1To page 2current pageTo page 4
[previous] [next]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint

Created: January 2, 2003
Revised: January 2, 2003

URL: http://webreference.com/programming/carts/chap7/3/3.html