How to block URLs in text area

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;
}

Thanks,
Elizabeth

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?

Hi Chris,

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;
}

Sincerely,
Elizabeth

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.

Thank you! I appreciate your help, and the updated script is working perfect now.

Thanks again for your help. In a future release, would Gravity Forms consider adding an option to disallow URLs in the form builder?

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.