Filter/restrict specific characters or strings in fields

Is there any way to filter/restrict/block specific characters or strings in fields?

Example: the text paragraph field
When somebody uses “http://” in it, the field displays an error message and the form can’t be sent

Other possible characters to block could be “:” or “/” .

I know it’s stupid since clickable links will also be created when the user just submits “mydomain.com”, but I’m doing this for a client who’s a little resistant to these kind of arguments.

Give a look at the filter gform_field_validation and the examples outlined there. It’ll let you put such restrictions in place.

1 Like

Many thanks.

Sorry I have to ask again.
This code sample comes from gform_field_validation. It’s to filter out certain words.
While it does work great for the specific words (in this example ‘one’ and ‘two’), it doesn’t work with strings WITHIN words, like www or http. How can I add a wildcard placeholder to the string to make this work for any “word” starting with ‘http’?
‘http*’ doesn’t work, I’ve tried already.

add_filter( 'gform_field_validation', function ( $result, $value, $form, $field ) {
    GFCommon::log_debug( __METHOD__ . '(): Running...' );
    // Only for Single Line Text and Paragraph fields.
    if ( $field->type == 'text' || $field->type == 'textarea' ) {
 
        if ( $result['is_valid'] ) {
            $stop_words = array( // List of words to not allow in lowercase.
                'one',
                'two',
            );
 
            if ( in_array( strtolower( $value ), $stop_words ) ) {
                GFCommon::log_debug( __METHOD__ . '(): Stop word detected: ' . $value );
                $result['is_valid'] = false;
                $result['message']  = 'Sorry, you used a not allowed word.';
            }
        }
 
    }
 
    return $result;
}, 10, 4 );

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