Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_filter('gform_validation_1', 'custom_validation');
- function custom_validation($validation_result) {
- // set the form validation to false
- $form = $validation_result["form"];
- $vin = trim($_POST['input_7']);
- // 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
- $validation_result["is_valid"] = false;
- $form["fields"][0]["failed_validation"] = true;
- $form["fields"][0]["validation_message"] = "Please make sure your VIN contains only letters and numbers and letters are all uppercase";
- }
- }
- $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) {
- } else {
- $validation_result["is_valid"] = false;
- $form["fields"][0]["failed_validation"] = true;
- $form["fields"][0]["validation_message"] = "The Calculated VIN Check Digit is Incorrect";
- }
- // END check if the 9th character in the string (the check digit) equals the calculated value
- $form_id = 1; // update to your form ID
- $field_id = 7; // update to your field ID
- $field_value = $vin; // VIN entry title
- //$form = RGFormsModel::get_form_meta($form_id);
- $field = RGFormsModel::get_field($form, $field_id);
- if(RGFormsModel::is_duplicate($form_id, $field, $field_value)) {
- $validation_result["is_valid"] = false;
- $form["fields"][0]["failed_validation"] = true;
- $form["fields"][0]["validation_message"] = "This is a duplicate VIN";
- }
- // check VIN length
- if (strlen($vin) != 17) {
- $validation_result["is_valid"] = false;
- $form["fields"][0]["failed_validation"] = true;
- $form["fields"][0]["validation_message"] = "Your VIN must be 17 digits in length";
- }
- // END check VIN length
- // check if the 1st and 2nd characters are WB
- if ($char[0] == W && $char[1] == B) {
- } elseif ($char[0] == w && $char[1] == b) {
- $validation_result["is_valid"] = false;
- $form["fields"][0]["failed_validation"] = true;
- $form["fields"][0]["validation_message"] = "Your VIN must begin with the letters WB and be all uppercase";
- } else {
- $validation_result["is_valid"] = false;
- $form["fields"][0]["failed_validation"] = true;
- $form["fields"][0]["validation_message"] = "Your VIN must begin with the letters WB";
- }
- // END check if the 1st and 2nd characters are WB
- // update the form in the validation result with the form object you modified
- $validation_result["form"] = $form;
- return $validation_result;
- }
Add Comment
Please, Sign In to add comment