Recently, my websites that use Gravity Forms are receiving manual spam submissions that contain website addresses in the comments field. I’ve tried the following code from a past forum thread, but it isn’t working. Is there any way to block submissions that enter a URL into a text field?
// add custom validation to form
add_filter( 'gform_field_validation_74_7', 'validate_phone_74_7', 10, 4 );
function validate_phone_74_7( $result, $value, $form, $field ) {
$nourl_pattern = '(http|https)';
if ( preg_match( $nourl_pattern, $value ) ) {
$result['is_valid'] = false;
$result['message'] = 'Message can not contain website addresses.';
}
return $result;
}
Hi Elizabeth. That code should work to prevent submission of form 74 if field 7 contains either http or https. Are you working with form 74 and field 7?
Thank you for getting back to me so quickly. Well, I made the obvious mistake of not replacing the form and field IDs when I copied the code above. I made the corrections below and it works perfectly now! My next question is how to edit the code below for sites with more than one form and/or field to protect.
// Add custom validation to form
add_filter( 'gform_field_validation_1_4', 'validate_input_1_4', 10, 4 );
function validate_input_1_4( $result, $value, $form, $field ) {
$nourl_pattern = '(http|https)';
if ( preg_match( $nourl_pattern, $value ) ) {
$result['is_valid'] = false;
$result['message'] = 'Message can not contain website addresses.';
}
return $result;
}
Hello. Have you tried it? That would be the true test.
The filter name gform_field_validation when used at gform_field_validation_1_4 targets field 4 in form 1.
The function name validate_input_1_4 could be whatever you like, so long as it does not exist already, and it’s a valid function name, and you change it on both lines.
These would work: no_more_urls validate_this_field prevent_spammers_from_submitting_links
But yes, to answer your question, it looks correct. Give it a try and let us know what happens.