Help on picking the correct field to work with [RESOLVED]

Hi All,

I wanted to implement a function that avoid urls in text fields. (Spammers)
I found a nice topic here:
But when I add this to my functions.php I dont get any results.

How do I change this to nice little code snippet to suit my form.
Can someone guide a novice coder like me… Please

How do I find my field ID so the code will work
(The code snippet from link… says Phone_validate …? Should be URL check - Not??)

I would like both… :laughing:
Phone check not longer than 10 numbers and not starting with other than +45
Trying to avoid all other countries than “home” (Denmark)

But main idea is to stop spammers adding urls: http|https|www

In advance

Thx
Netz

HI Netz,

This snippet will prevent people from adding URLs to Single Line Text and Paragraph fields, no need to put any field id’s or edit anything, just copy/paste to your site:

add_filter( 'gform_field_validation', function ( $result, $value, $form, $field ) {
	// Only for Single Line Text and Paragraph fields.
	if ( $field->type == 'text' || $field->type == 'textarea' ) {

		$has_url = false;

		if ( preg_match ( '/www\.|http:|https:\/\/[a-z0-9_]+([\-\.]{1}[a-z_0-9]+)*\.[_a-z]{2,5}'.'((:[0-9]{1,5})?\/.*)?$/i', $value ) ) {
			$has_url = true;
		}

		if ( $has_url ) {
			$result['is_valid'] = false;
			$result['message']  = empty( $field->errorMessage ) ? 'Sorry, URL not allowed!' : $field->errorMessage;
		}

	}

	return $result;
}, 10, 4 );

And this one will add a new “Custom” phone format to the Phone filed settings, you need to select it and save the field settings. Then the field will require the user a 10 digits phone, prefix will be already added:

add_filter( 'gform_phone_formats', 'gf_custom_phone_format' );
function gf_custom_phone_format( $phone_formats ) {
    $phone_formats['custom'] = array(
        'label'       => 'Custom',
        'mask'        => '+459999999999',
        'regex'       => '/^\+45[0-9]{10}$/',
        'instruction' => '',
    );
 
    return $phone_formats;
}
1 Like

@sacom
Wow… You are a true hero. Thanks a million! And you are fast.
Thumps up and a million thanks for this perfect help!
You are a keeper :laughing:

2 Likes