Mathy Funtimes

While I usually do a bit of JS and PHP on occasion, I sometimes get thrown a doozy. In this case, it was coding up a custom form field that validates a unique checksum-verified airmile member card number. This was certainly outside my experience, but I managed it, also finally finding a use for all that math I forgot learned in high school.

Here’s a breakdown of what I did, starting with the javascript that first passes the form field through a few important checks first (also i make use of jquery in addition to vanilla javascript):

function validate_form(thisform){
var airmile = $.trim($("input[name='Field5833165']").val());
if(airmile!=''){
airmile = airmile.replace(/ /g,'');
if($.isNumeric(airmile)){
if(airmile.length==11){

 

The above code basically strips blank spaces, checks if empty, checks if the field contains anything except numerical characters (0-9) and verifies that the field is the correct length. If any of these fail, an will popup (see below).


$.ajax({
type: "GET",
dataType: 'jsonp',
data: 'num='+airmile,
jsonp: 'jsonp_callback',
url: 'https://sample.com/airmiles/airmile.php',
success: function (data) {
if(data.success==0){
alert('Your airline miles number is invalid.');
return false;
}else{
// Airline number is valid... continue
if(CheckForm(thisform)){
thisform.submit();
}
}
}
});
// prevent script form submitting when ajax is processing
return false;
}else{
// Not 11 characters
alert('Your airline miles number is invalid.');
return false;
}
}else{
// Not numeric
alert('Your airline miles number is invalid.');
return false;
}
}else{
// They didn't enter a number, continue
return true;
}
}

Above, we’re making a JSON callback to a PHP script that will run all the fancy arithmatic to validate the miles card number. It won’t even bother with the PHP unless it passed all the initial checks in the JS. If success, it procedes to submit the form, if not, it throws errors. Now here’s the PHP script, which does the fancy math:


function send_response($resp){
return $_GET['jsonp_callback'] . '(' . json_encode($resp) . ');';
}
function airmile_check_digit($digits){
$digits =(string)$digits;
$total_sum = ($digits{0}*2) + ($digits{1}*6) + ($digits{2}*4) + ($digits{3}*3) + ($digits{4}*8) + ($digits{5}*7) + ($digits{6}*6) + ($digits{7}*4) + ($digits{8}*7) + ($digits{9}*3);

 

The fun math begins! Above you see that we’re turning the digits to an array so we can access individual numbers. Then we multiply each digit in the string by a unique value, provided by the air miles card company as part of their checksum algorithm.


$divisor = 11;
$quotient = floor($total_sum/$divisor);
$remainder = $total_sum%$divisor;
if ($remainder $chk_digit = 0;
}else {
$chk_digit = floor(11-$remainder);
}
//see if check matches the final digit

if ($chk_digit==$digits{10}) {
$response = array(‘success’=>1);
//echo “Card number is GOOD

“;
}else{
$response = array(‘success’=>0);
//echo “Card number is BAD
“;
}

 

This previous step takes that large sum and divides it by 11. We then run a modulo operation to reveal the remainder, then subtract it from 11. If this number is less than 2, the check digit = 0, otherwise it will equal the check digit and be a success. That’s it! Any string of numbers should now validate the final checksum digit.


// Individual Registration
if( location.href.indexOf('/reg_new/register.asp') >0 && $("form[name='registrationForm']").size()>0 ){

$(“#Image1”).click(function(){
var theform = $(this).closest(‘form’).get(0);
return validate_form(theform);
});

}
// Individual Registration
if( location.href.indexOf(‘/donorReg/donorBilling.asp’) >0 && $(“form[name=’billingForm’]”).size()>0 ){

$(“input[name=’imageField’]”).click(function(){
var theform = $(this).closest(‘form’).get(0);
return validate_form(theform);
});

 

Lastly, we return to our JS script, where we are performing the functions that actually start this script, In this case, our Airmiles number is on a donation and a registration form, running when the submit button is clicked.