Blocking URL's in paragraph text fields

Is the script in the topic below still the preferred method to block url entries in text fields and does it work with the latest version of gravity forms?

Hi Shaun,

Yes, the provided code is still effective in blocking website URLs from the paragraph field.

add_filter( 'gform_field_validation_1_74', 'validate_input_1_74', 10, 4 );

function validate_input_1_74( $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;
}

Have you had a chance to test it?

I was able to test it and confirm that it’s working in my test environment, thanks.

Is there a benefit (other than an error message appearing) vs. using conditional logic to hide the submit button if my message field contains “http” or “https”. That’s what I was doing as a quicker work around.

Hi Shaun,

We can use jQuery code to hide the submit button if the paragraph field contains any URL. However, it might not work smoothly, so you may see the button while typing.

I recommend using the first option, but if you prefer, you can use the following code as well. It only performs front-end validation, not server-side validation.

jQuery(document).ready(function($) {
    // Function to check the text and update button visibility
    function updateButtonVisibility() {
        var messageText = $('#input_1_74').val() || '';
        
        if (messageText.toLowerCase().includes('http') || messageText.toLowerCase().includes('https')) {
            $('#gform_submit_button_1').css('display', 'none');
        } else {
            $('#gform_submit_button_1').css('display', 'block');
        }
    }

    $(document).on('change keyup paste', '#input_1_74', updateButtonVisibility);
    
    if ($('#input_1_74').length) {
        updateButtonVisibility();
    }
});

Give it a try, and let me know how that goes!

This works, thanks for the suggestion.

It’s a shame that Gravity forms doesn’t have built in toggle to prevent URL’s in fields as it seems like a common type of spam for contact forms.

You can suggest this feature directly to the product management team for consideration when planning future releases on the Gravity Forms product roadmap page.