<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Credit Card Validation - timmerstidbits.com</title>
</head>

<body>
<?php if ($_POST['action'] !== "process") { ?>
<form name="frmCC" action="creditcard.php" method="post" id="frmCC">
    <div align="left">
      <table width="100%" border="0" cellspacing="0" cellpadding="4">
        <tr>
          <td width="18%">Cardholders name: </td>
          <td width="82%"><input name="ccName" type="text" value="<?=$ccName?>" /></td>
        </tr>
        <tr>
          <td>Card number: </td>
          <td><input name="ccNum" type="text" value="<?=$ccNum?>" /></td>
        </tr>
        <tr>
          <td>Card type: </td>
          <td><select name="ccType">
            <option value="#" selected="selected">SELECT</option>
            <option value="MasterCard">Mastercard</option>
            <option value="Visa">Visa</option>
            <option value="Amex">Amex</option>
            <option value="Discover">Discover</option>
          </select></td>
        </tr>
        <tr>
          <td>Expiry Date: </td>
          <td><select name="ccExpM">
            <option selected>MM</option>
            <?php
    
for($i 1$i 13$i++)
        { echo 
'<option>' $i '</option>'; }
    
?>
          </select>
            <select name="ccExpY">
            <option selected>YYYY</option>
              <?php
    
for($i 2007$i 2013$i++)
        { echo 
'<option>' $i '</option>'; }
    
?>
            </select></td>
        </tr>
        <tr>
          <td>CVV Code: </td>
          <td><input name="ccCVV" type="text" id="ccCVV" value="<?=$ccCVV?>" size="5" maxlength="5" /></td>
        </tr>
        <tr>
          <td><input type="hidden" name="action" value="process" /></td>
          <td><input type="submit" name="submit" value="Validate" /></td>
        </tr>
      </table>
    </div>
  </form>
  <script language="JavaScript" type="text/javascript">
    //You should create the validator only after the definition of the HTML form
      var frmvalidator  = new Validator("frmCC");
      frmvalidator.addValidation("ccName","req","Please enter your the name on your credit card.");
      frmvalidator.addValidation("ccName","maxlen=40",
        "Max length for your name is 40 characters.");
                          
      frmvalidator.addValidation("ccNum","maxlen=16", "Your credit card must be 16 characters or less.");
      frmvalidator.addValidation("ccNum","numeric", "Only Numbers are allowed in this field.");
      frmvalidator.addValidation("ccNum","minlen=15", "You must enter at least 15-16 characters.");
      
      frmvalidator.addValidation("ccType","alpha", "You must select your credit card type.");
      
      frmvalidator.addValidation("ccExpM","numeric", "You must select your credit card's expiration month.");
      frmvalidator.addValidation("ccExpY","numeric", "You must select your credit card's expiration year.");
      frmvalidator.addValidation("ccCVV", "numeric", "You must enter the CVV Code located on the back of your card. (Last 3 Digits)");
      frmvalidator.addValidation("ccCVV", "minlen=3","CVV Code must be at least 3 characters.");
      frmvalidator.addValidation("ccCVV", "maxlen=5","CVV Code has a maximum length of 5 characters.");
    
    </script>
    <?php
    
} else {
        include(
"class.creditcard.php"); //include the validation script
    
        
$search  = array("-"" ");
        
$replace = array("""");
    
        
// get the POST variables for the transaction we're trying to process FORMATTING
        
$ccType         $_POST['ccType'];
        
$ccNum           str_replace($search$replacetrim($_POST['ccNum']));
        
$ccName            trim($_POST['ccName']);
        
$ccExpM         $_POST['ccExpM'];
        
$ccExpY         $_POST['ccExpY'];
        
$ccCVV          $_POST['ccCVV'];
                            
      if (!
checkCreditCard ($ccNum$ccType$ccerror$ccerrortext)) {
        echo 
$ccerrortext;
        exit();
      } else { 
//card is good, process it

        //encrypt credit card info
        
$cc_no_enc             SafeNumber($ccNum'x'12);
        
$firstFour             substr($ccNum04);
    
$lastTwelve           substr($ccNum, -1212);

    
// $firstFour is displayed on the validation results page
    // $lastTwelve is not displayed and contains the remaining 12 (or 11) digits
    // This is where you would add them to a DB or email them to the proper person
    // ONLY USE THIS SCRIPT IF IT IS BEHIND A SECURE SSL Certificate AS DISCUSSED
     
?>
         
        <h2 align="left">Validation Results</h2>
            <div align="left"><b>Name: </b><?php echo $_POST['ccName']; ?><br>
              <b>Number: </b><?php echo $cc_no_enc?><br>
              <b>Type: </b><?php echo $ccType?><br>
              <b>Expires: <?php echo $ccExpM?>/<?php echo $ccExpY?></b><br>
              <br>
            </div>
    <?php
        
}
     } 
    
?>
</body>
</html>