Guest User

Untitled

a guest
Jan 23rd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. add_filter('gform_validation_1', 'custom_validation');
  2. function custom_validation($validation_result) {
  3.  
  4. // set the form validation to false
  5. $form = $validation_result["form"];
  6.  
  7. $vin = trim($_POST['input_7']);
  8.  
  9. // setup array of letter values
  10. $value['A'] = 1;
  11. $value['B'] = 2;
  12. $value['C'] = 3;
  13. $value['D'] = 4;
  14. $value['E'] = 5;
  15. $value['F'] = 6;
  16. $value['G'] = 7;
  17. $value['H'] = 8;
  18. $value['J'] = 1;
  19. $value['K'] = 2;
  20. $value['L'] = 3;
  21. $value['M'] = 4;
  22. $value['N'] = 5;
  23. $value['P'] = 7;
  24. $value['R'] = 9;
  25. $value['S'] = 2;
  26. $value['T'] = 3;
  27. $value['U'] = 4;
  28. $value['V'] = 5;
  29. $value['W'] = 6;
  30. $value['X'] = 7;
  31. $value['Y'] = 8;
  32. $value['Z'] = 9;
  33.  
  34. // setup digit weights
  35. $weight[0] = 8; // 1st position
  36. $weight[1] = 7;
  37. $weight[2] = 6;
  38. $weight[3] = 5;
  39. $weight[4] = 4;
  40. $weight[5] = 3;
  41. $weight[6] = 2;
  42. $weight[7] = 10;
  43. $weight[8] = 0; // 9th position, this is the check digit
  44. $weight[9] = 9;
  45. $weight[10] = 8;
  46. $weight[11] = 7;
  47. $weight[12] = 6;
  48. $weight[13] = 5;
  49. $weight[14] = 4;
  50. $weight[15] = 3;
  51. $weight[16] = 2; // 17th position
  52.  
  53. $char = str_split($vin); // split string into character array
  54. $total = 0;
  55.  
  56. // loop though each character of the vin
  57. for ($i = 0; $i < 17; $i++) {
  58. if (is_numeric($char[$i])) {
  59. // use number
  60. // update total
  61. $total = $total + ($char[$i] * $weight[$i]);
  62. } elseif (array_key_exists($char[$i], $value)) {
  63. // use value of letter
  64. // update total
  65. $total = $total + ($value[$char[$i]] * $weight[$i]);
  66. } else {
  67. // error illegal character used
  68. $validation_result["is_valid"] = false;
  69. $form["fields"][0]["failed_validation"] = true;
  70. $form["fields"][0]["validation_message"] = "Please make sure your VIN contains only letters and numbers and letters are all uppercase";
  71. }
  72. }
  73.  
  74. $mod = $total % 11; // find remainder after dividing by 11
  75.  
  76. // if mod is 10 set the check_digit to X
  77. if ($mod == 10) {
  78. $checkDigit = 'X';
  79. } else {
  80. $checkDigit = $mod;
  81. }
  82.  
  83. // check if the 9th character in the string (the check digit) equals the calculated value
  84. if ($char[8] == $checkDigit) {
  85. } else {
  86. $validation_result["is_valid"] = false;
  87. $form["fields"][0]["failed_validation"] = true;
  88. $form["fields"][0]["validation_message"] = "The Calculated VIN Check Digit is Incorrect";
  89. }
  90. // END check if the 9th character in the string (the check digit) equals the calculated value
  91.  
  92. $form_id = 1; // update to your form ID
  93. $field_id = 7; // update to your field ID
  94. $field_value = $vin; // VIN entry title
  95.  
  96. //$form = RGFormsModel::get_form_meta($form_id);
  97. $field = RGFormsModel::get_field($form, $field_id);
  98.  
  99. if(RGFormsModel::is_duplicate($form_id, $field, $field_value)) {
  100.  
  101. $validation_result["is_valid"] = false;
  102. $form["fields"][0]["failed_validation"] = true;
  103. $form["fields"][0]["validation_message"] = "This is a duplicate VIN";
  104.  
  105. }
  106.  
  107. // check VIN length
  108. if (strlen($vin) != 17) {
  109. $validation_result["is_valid"] = false;
  110. $form["fields"][0]["failed_validation"] = true;
  111. $form["fields"][0]["validation_message"] = "Your VIN must be 17 digits in length";
  112. }
  113. // END check VIN length
  114.  
  115. // check if the 1st and 2nd characters are WB
  116. if ($char[0] == W && $char[1] == B) {
  117. } elseif ($char[0] == w && $char[1] == b) {
  118. $validation_result["is_valid"] = false;
  119. $form["fields"][0]["failed_validation"] = true;
  120. $form["fields"][0]["validation_message"] = "Your VIN must begin with the letters WB and be all uppercase";
  121. } else {
  122. $validation_result["is_valid"] = false;
  123. $form["fields"][0]["failed_validation"] = true;
  124. $form["fields"][0]["validation_message"] = "Your VIN must begin with the letters WB";
  125. }
  126. // END check if the 1st and 2nd characters are WB
  127.  
  128. // update the form in the validation result with the form object you modified
  129. $validation_result["form"] = $form;
  130.  
  131. return $validation_result;
  132.  
  133. }
Add Comment
Please, Sign In to add comment