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…
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
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: