<?xml version="1.0"?><!-- generator="bbPress" -->

<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
>

<channel>
<title>MachForm Community Forums Topic: HOW TO: Add Mod-10 Credit Card validation to your form.</title>
<link>https://www.machform.com/forums/</link>
<description>MachForm Community Forums Topic: HOW TO: Add Mod-10 Credit Card validation to your form.</description>
<language>en</language>
<pubDate>Mon, 04 May 2026 00:28:58 +0000</pubDate>

<item>
<title>aolmos on "HOW TO: Add Mod-10 Credit Card validation to your form."</title>
<link>https://www.machform.com/forums/topic/how-to-add-mod-10-credit-card-validation-to-your-form#post-21000</link>
<pubDate>Fri, 09 May 2014 21:59:56 +0000</pubDate>
<dc:creator>aolmos</dc:creator>
<guid isPermaLink="false">21000@https://www.machform.com/forums/</guid>
<description>&#60;p&#62;What about version 4?
&#60;/p&#62;</description>
</item>
<item>
<title>tmparisi on "HOW TO: Add Mod-10 Credit Card validation to your form."</title>
<link>https://www.machform.com/forums/topic/how-to-add-mod-10-credit-card-validation-to-your-form#post-16815</link>
<pubDate>Thu, 17 Jan 2013 04:34:29 +0000</pubDate>
<dc:creator>tmparisi</dc:creator>
<guid isPermaLink="false">16815@https://www.machform.com/forums/</guid>
<description>&#60;p&#62;Hi aolmos, unfortunately I cannot help you with this.  I have never used v2.  Best of luck!
&#60;/p&#62;</description>
</item>
<item>
<title>aolmos on "HOW TO: Add Mod-10 Credit Card validation to your form."</title>
<link>https://www.machform.com/forums/topic/how-to-add-mod-10-credit-card-validation-to-your-form#post-16103</link>
<pubDate>Mon, 01 Oct 2012 17:00:49 +0000</pubDate>
<dc:creator>aolmos</dc:creator>
<guid isPermaLink="false">16103@https://www.machform.com/forums/</guid>
<description>&#60;p&#62;How can I include this code in machform 2? We have customized our machform and we can't switch to machform 3
&#60;/p&#62;</description>
</item>
<item>
<title>yuniar on "HOW TO: Add Mod-10 Credit Card validation to your form."</title>
<link>https://www.machform.com/forums/topic/how-to-add-mod-10-credit-card-validation-to-your-form#post-14611</link>
<pubDate>Mon, 07 May 2012 11:47:45 +0000</pubDate>
<dc:creator>yuniar</dc:creator>
<guid isPermaLink="false">14611@https://www.machform.com/forums/</guid>
<description>&#60;p&#62;Thank you for sharing this!
&#60;/p&#62;</description>
</item>
<item>
<title>tmparisi on "HOW TO: Add Mod-10 Credit Card validation to your form."</title>
<link>https://www.machform.com/forums/topic/how-to-add-mod-10-credit-card-validation-to-your-form#post-14608</link>
<pubDate>Mon, 07 May 2012 06:39:31 +0000</pubDate>
<dc:creator>tmparisi</dc:creator>
<guid isPermaLink="false">14608@https://www.machform.com/forums/</guid>
<description>&#60;p&#62;For those of you that take credit card numbers in your forms, here is a simple way to set up CC validation in your form that ultimately checks for several factors including, length, prefix, and mod-10 algorithm.  The following code is adapted from:  &#60;a href=&#34;http://www.braemoor.co.uk/software/creditcard.php&#34; rel=&#34;nofollow&#34;&#62;http://www.braemoor.co.uk/software/creditcard.php&#60;/a&#62;&#60;/p&#62;
&#60;p&#62;Open up your post_functions.php file in your include folder and find the following lines of code:&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;if(!empty($row[&#38;#39;form_redirect_enable&#38;#39;])){
 $form_redirect = $row[&#38;#39;form_redirect&#38;#39;];
 }&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;Right below this code, add the following verification script:&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;//custom code
 //====================START CC Validation=======================
 if ($form_id == 15 &#124;&#124; $form_id == 3 &#124;&#124; $form_id == 4 &#124;&#124; $form_id == 10 &#124;&#124; $form_id == 11) {  //form numbers that have text fields that you want to perform CC validation
 if($form_id == 15)  //if form #15, set the credit card type dropdown and the credit card number fields according to the element numbers for that form
 {
 $cc_validate_id = 1;  //set credit card number field
 $cc_validate_type = 2;  //set credit card type field
 }
 if($form_id == 3)
 {
 $cc_validate_id = 28;
 $cc_validate_type = 17;
 }
 if($form_id == 4)
 {
 $cc_validate_id = 29;
 $cc_validate_type = 17;
 }
 if($form_id == 10)
 {
 $cc_validate_id = 28;
 $cc_validate_type = 17;
 }
 if($form_id == 11)
 {
 $cc_validate_id = 29;
 $cc_validate_type = 17;
 }

 $failcheck = 0;
 if (!empty($table_data[&#38;#39;element_&#38;#39; . $cc_validate_id])) {

 if($table_data[&#38;#39;element_&#38;#39; . $cc_validate_type] == 1) //check if credit card dropdown type is 1
 {
 $cardname = &#38;#39;american express&#38;#39;; //if so, set $cardname equal to american express
 }
 else if($table_data[&#38;#39;element_&#38;#39; . $cc_validate_type] == 2)
 {
 $cardname = &#38;#39;visa&#38;#39;;
 }
 else if($table_data[&#38;#39;element_&#38;#39; . $cc_validate_type] == 3)
 {
 $cardname = &#38;#39;mastercard&#38;#39;;
 }
 else if($table_data[&#38;#39;element_&#38;#39; . $cc_validate_type] == 4)
 {
 $cardname = &#38;#39;discover&#38;#39;;
 }
 else
 {
 $cardname = &#38;#39;&#38;#39;;
 }

 $cardnumber = $table_data[&#38;#39;element_&#38;#39; . $cc_validate_id]; //set $cardnumber equal to the credit card number entered by user

 $cards = array ( array (&#38;#39;name&#38;#39; =&#38;gt; &#38;#39;American Express&#38;#39;,
&#38;#39;length&#38;#39; =&#38;gt; &#38;#39;15&#38;#39;,
&#38;#39;prefixes&#38;#39; =&#38;gt; &#38;#39;34,37&#38;#39;,
 &#38;#39;checkdigit&#38;#39; =&#38;gt; true
 ),
 array (&#38;#39;name&#38;#39; =&#38;gt; &#38;#39;Discover&#38;#39;,
&#38;#39;length&#38;#39; =&#38;gt; &#38;#39;16&#38;#39;,
&#38;#39;prefixes&#38;#39; =&#38;gt; &#38;#39;6011,622,64,65&#38;#39;,
 &#38;#39;checkdigit&#38;#39; =&#38;gt; true
 ),
 array (&#38;#39;name&#38;#39; =&#38;gt; &#38;#39;MasterCard&#38;#39;,
&#38;#39;length&#38;#39; =&#38;gt; &#38;#39;16&#38;#39;,
&#38;#39;prefixes&#38;#39; =&#38;gt; &#38;#39;51,52,53,54,55&#38;#39;,
 &#38;#39;checkdigit&#38;#39; =&#38;gt; true
 ),
 array (&#38;#39;name&#38;#39; =&#38;gt; &#38;#39;VISA&#38;#39;,
&#38;#39;length&#38;#39; =&#38;gt; &#38;#39;16&#38;#39;,
&#38;#39;prefixes&#38;#39; =&#38;gt; &#38;#39;4&#38;#39;,
 &#38;#39;checkdigit&#38;#39; =&#38;gt; true
 ),
 array (&#38;#39;name&#38;#39; =&#38;gt; &#38;#39;VISA Electron&#38;#39;,
&#38;#39;length&#38;#39; =&#38;gt; &#38;#39;16&#38;#39;,
&#38;#39;prefixes&#38;#39; =&#38;gt; &#38;#39;417500,4917,4913,4508,4844&#38;#39;,
 &#38;#39;checkdigit&#38;#39; =&#38;gt; true
 )
 );

 $ccErrorNo = 0;

 $ccErrors [0] = &#38;quot;Unknown card type&#38;quot;;
 $ccErrors [1] = &#38;quot;No card number provided&#38;quot;;
 $ccErrors [2] = &#38;quot;Credit card number has invalid format&#38;quot;;
 $ccErrors [3] = &#38;quot;Credit card number is invalid&#38;quot;;
 $ccErrors [4] = &#38;quot;Credit card number is wrong length&#38;quot;;

 // Establish card type
 $cardType = -1;
 for ($i=0; $i&#38;lt;sizeof($cards); $i++) {

 // See if it is this card (ignoring the case of the string)
 if (strtolower($cardname) == strtolower($cards[$i][&#38;#39;name&#38;#39;])) {
 $cardType = $i;
 break;
 }
 }

 // If card type not found, report an error
 if ($cardType == -1 &#38;#38;&#38;#38; $failcheck == 0) {
 $errornumber = 0;
$errortext = $ccErrors [$errornumber];
 $error_elements[$cc_validate_id] = $ccErrors [$errornumber];
 $failcheck = 1;
 }

 // Ensure that the user has provided a credit card number
 if (strlen($cardnumber) == 0 &#38;#38;&#38;#38; $failcheck == 0) {
 $errornumber = 1;
$errortext = $ccErrors [$errornumber];
 $error_elements[$cc_validate_id] = $ccErrors [$errornumber];
 $failcheck = 1;
 }

 // Remove any spaces from the credit card number
 $cardNo = str_replace (&#38;#39; &#38;#39;, &#38;#39;&#38;#39;, $cardnumber); 

// Check that the number is numeric and of the right sort of length.
 if (!preg_match(&#38;quot;/^[0-9]{13,19}$/&#38;quot;,$cardNo) &#38;#38;&#38;#38; $failcheck == 0) {
 $errornumber = 2;
$errortext = $ccErrors [$errornumber];
 $error_elements[$cc_validate_id] = $ccErrors [$errornumber];
 $failcheck = 2;
 }

 // Now check the modulus 10 check digit - if required
 if ($cards[$cardType][&#38;#39;checkdigit&#38;#39;] &#38;#38;&#38;#38; $failcheck == 0) {
 $checksum = 0; // running checksum total
 $mychar = &#38;quot;&#38;quot;; // next char to process
 $j = 1; // takes value of 1 or 2

 // Process each digit one by one starting at the right
 for ($i = strlen($cardNo) - 1; $i &#38;gt;= 0; $i--) {

 // Extract the next digit and multiply by 1 or 2 on alternative digits.
$calc = $cardNo{$i} * $j;

 // If the result is in two digits add 1 to the checksum total
 if ($calc &#38;gt; 9) {
 $checksum = $checksum + 1;
 $calc = $calc - 10;
 }

 // Add the units element to the checksum total
 $checksum = $checksum + $calc;

 // Switch the value of j
 if ($j ==1) {$j = 2;} else {$j = 1;};
 } 

// All done - if checksum is divisible by 10, it is a valid modulus 10.
 // If not, report an error.
 if ($checksum % 10 != 0 &#38;#38;&#38;#38; $failcheck == 0) {
 $errornumber = 3;
$errortext = $ccErrors [$errornumber];
 $error_elements[$cc_validate_id] = $ccErrors [$errornumber];
 $failcheck = 3;
 }
 } 

// The following are the card-specific checks we undertake.

 // Load an array with the valid prefixes for this card
 $prefix = explode(&#38;#39;,&#38;#39;,$cards[$cardType][&#38;#39;prefixes&#38;#39;]);

 // Now see if any of them match what we have in the card number
$PrefixValid = false;
for ($i=0; $i&#38;lt;sizeof($prefix); $i++) {
 $exp = &#38;#39;/^&#38;#39; . $prefix[$i] . &#38;#39;/&#38;#39;;
 if (preg_match($exp,$cardNo)) {
 $PrefixValid = true;
 break;
 }
 }

 // If it isn&#38;#39;t a valid prefix there&#38;#39;s no point at looking at the length
 if (!$PrefixValid &#38;#38;&#38;#38; $failcheck == 0) {
 $errornumber = 3;
$errortext = $ccErrors [$errornumber];
 $error_elements[$cc_validate_id] = $ccErrors [$errornumber];
 $failcheck = 3;
 }

 // See if the length is valid for this card
 $LengthValid = false;
 $lengths = explode(&#38;#39;,&#38;#39;,$cards[$cardType][&#38;#39;length&#38;#39;]);
 for ($j=0; $j&#38;lt;sizeof($lengths); $j++) {
 if (strlen($cardNo) == $lengths[$j]) {
 $LengthValid = true;
 break;
 }
 }

 // See if all is OK by seeing if the length was valid.
if (!$LengthValid &#38;#38;&#38;#38; $failcheck == 0) {
 $errornumber = 4;
$errortext = $ccErrors [$errornumber];
 $error_elements[$cc_validate_id] = $ccErrors [$errornumber];
 $failcheck = 4;
 }; 

 }
 }
 //====================END CC Validation=======================&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;This is probably not perfect nor is it most likely the most efficient method, but it is tested and does work.  I hope it can be of some use for others out there!
&#60;/p&#62;</description>
</item>

</channel>
</rss>
