Allowing www. in "website" field [RESOLVED]

How can i allow users to enter their email address in a contact form as www.bbc.co.uk for example and not have it return the error indicating it doesn’rt start with https://

I think most people filling out the form would rather just start their email address with www. and not https://

Hi Stuart,

To allow “www” in the website field, please try adding the following code to the child theme’s functions.php file or use a Code Snippets plugin. After adding the code, the validation will allow “www” as shown in the screenshot below.

add_filter( 'gform_field_validation', function( $result, $value, $form, $field ) {
	if ( $field->type !== 'website' ) {
		return $result;
	}

	if ( ! $result['is_valid'] && preg_match( '/^www\./i', trim( $value ) ) ) {
		$url_with_protocol = 'https://' . trim( $value );

		if ( GFCommon::is_valid_url( $url_with_protocol ) ) {
			$result['is_valid'] = true;
			$result['message']  = '';
		}
	}

	return $result;
}, 10, 4 );

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

1 Like

Brilliant - all sorted

Thanks !

2 Likes