I will chalk this up to my weak abilities in mathematics for lack of being able to find a working solution to this script. Highly unlikely it is a bug in these php functions.
The form should only every validate and allow submission if the first five characters for the $value string are not a match in the $master string. Going in circles trying to get this to function properly. Thank you in advance for any tips or advice.
Example 1:
Joey ($value)
Joey123 ($master)
In the above instance, the form should not validate if the first 0-5 characters in the string are a match. But, the above example allows the form to validate.
Example 2:
Joey ($value)
Joey ($master)
In the above instance, the form returns false and is invalid. As expected, springing the false validation message.
I believe the issue lies in the first if() check on$result['is_valid']
, so I am running various tests with and without this initial statement.
add_filter('gform_field_validation_1_4', function ( $result, $value, $form, $field ) {
$value = substr($value, 0, 5);
$master = substr(rgpost('input_5'), 0, 5);
if ($result['is_valid'] && strpos($value, $master) !== false) {
$result['is_valid'] = false;
$result['message'] = 'If you are having trouble submitting this form, please call us directly.';
}
echo 'Master: ' . $master;
echo 'Value: ' . $value;
return $result;
}, 10, 4 );
2
Answers
Well, I believe I've done the best I could on this, but it does seem to be working as expected (although could be some shortcomings if a first and last name have the first same 5 characters). I went with the
str_contains()
function (php8) and then checked for an identical match at any length as these were the two primary types of spam coming through, and the spam has now ceased!Thank you to mikerojas for setting me on the right track of fixing my original flawed code.
I believe you need to reverse the params in
strpos
like so:per the php docs the order is "haystack" then "needle": https://www.php.net/manual/en/function.strpos.php