Created
November 7, 2013 03:15
-
-
Save bMinaise/7348375 to your computer and use it in GitHub Desktop.
Gravity Form - Age Verification Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// 1 - Tie our validation function to the 'gform_validation' hook | |
add_filter('gform_validation_47', 'validate_vin'); | |
function validate_vin($validation_result) { | |
// 2 - Get the form object from the validation result | |
$form = $validation_result["form"]; | |
// 3 - Get the current page being validated | |
$current_page = rgpost('gform_source_page_number_' . $form['id']) ? rgpost('gform_source_page_number_' . $form['id']) : 1; | |
// 4 - Loop through the form fields | |
foreach($form['fields'] as &$field){ | |
// 5 - If the field does not have our designated CSS class, skip it | |
if(strpos($field['cssClass'], 'validate-vin') === false) | |
continue; | |
// 6 - Get the field's page number | |
$field_page = $field['pageNumber']; | |
// 7 - Check if the field is hidden by GF conditional logic | |
$is_hidden = RGFormsModel::is_field_hidden($form, $field, array()); | |
// 8 - If the field is not on the current page OR if the field is hidden, skip it | |
if($field_page != $current_page || $is_hidden) | |
continue; | |
// 9 - Get the submitted value from the $_POST | |
$field_value = rgpost("input_{$field['id']}"); | |
// 10 - Make a call to your validation function to validate the value | |
$is_valid = is_vin($field_value); | |
// 11 - If the field is valid we don't need to do anything, skip it | |
if($is_valid) | |
continue; | |
// 12 - The field field validation, so first we'll need to fail the validation for the entire form | |
$validation_result['is_valid'] = false; | |
// 13 - Next we'll mark the specific field that failed and add a custom validation message | |
$field['failed_validation'] = true; | |
$field['validation_message'] = 'The VIN number you have entered is not valid.'; | |
} | |
// 14 - Assign our modified $form object back to the validation result | |
$validation_result['form'] = $form; | |
// 15 - Return the validation result | |
return $validation_result; | |
} | |
function is_vin($vin) { | |
$vin = strtoupper(trim($vin)); | |
// check VIN length | |
if (strlen($vin) != 17) { | |
// error VIN is not 17 characters long | |
return false; | |
} | |
// setup array of letter values | |
$value['A'] = 1; | |
$value['B'] = 2; | |
$value['C'] = 3; | |
$value['D'] = 4; | |
$value['E'] = 5; | |
$value['F'] = 6; | |
$value['G'] = 7; | |
$value['H'] = 8; | |
$value['J'] = 1; | |
$value['K'] = 2; | |
$value['L'] = 3; | |
$value['M'] = 4; | |
$value['N'] = 5; | |
$value['P'] = 7; | |
$value['R'] = 9; | |
$value['S'] = 2; | |
$value['T'] = 3; | |
$value['U'] = 4; | |
$value['V'] = 5; | |
$value['W'] = 6; | |
$value['X'] = 7; | |
$value['Y'] = 8; | |
$value['Z'] = 9; | |
// setup digit weights | |
$weight[0] = 8; // 1st position | |
$weight[1] = 7; | |
$weight[2] = 6; | |
$weight[3] = 5; | |
$weight[4] = 4; | |
$weight[5] = 3; | |
$weight[6] = 2; | |
$weight[7] = 10; | |
$weight[8] = 0; // 9th position, this is the check digit | |
$weight[9] = 9; | |
$weight[10] = 8; | |
$weight[11] = 7; | |
$weight[12] = 6; | |
$weight[13] = 5; | |
$weight[14] = 4; | |
$weight[15] = 3; | |
$weight[16] = 2; // 17th position | |
$char = str_split($vin); // split string into character array | |
$total = 0; | |
// loop though each character of the vin | |
for ($i = 0; $i < 17; $i++) { | |
if (is_numeric($char[$i])) { | |
// use number | |
// update total | |
$total = $total + ($char[$i] * $weight[$i]); | |
} elseif (array_key_exists($char[$i], $value)) { | |
// use value of letter | |
// update total | |
$total = $total + ($value[$char[$i]] * $weight[$i]); | |
} else { | |
// error illegal character used | |
return false; | |
} | |
} | |
$mod = $total % 11; // find remainder after dividing by 11 | |
// if mod is 10 set the check_digit to X | |
if ($mod == 10) { | |
$checkDigit = 'X'; | |
} else { | |
$checkDigit = $mod; | |
} | |
// check if the 9th character in the string (the check digit) equals the calculated value | |
if ($char[8] == $checkDigit) { | |
// VIN is valid | |
return true; | |
} else { | |
// VIN is not valid | |
return false; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment