Gromit
Gromit

Reputation: 1

Validating textarea with 2 conditions Contact Form 7

I'd like to execute 2 tests on a textarea within a CF7 form.

First: the textarea does not contain an url

Second: the same textarea does not contain email address

I tried this code:

function custom_textarea_validation_filter($result, $tag) {
$type = $tag['type'];
$name = $tag['name'];
if($name == 'yourmessage') {
$value = $_POST[$name];
if(preg_match("/\b(?:(?:https?|ftp|http):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$value)||preg_match("#^[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,8}$#",$value)){ 

    $result->invalidate( $tag, "Merci de ne pas inclure de liens, url, ou adresse email !" );
                }
}
return $result;
}
add_filter('wpcf7_validate_textarea','custom_textarea_validation_filter', 10, 2);
add_filter('wpcf7_validate_textarea*', 'custom_textarea_validation_filter', 10, 2);

yourmessage is the textarea I want to test

But it does not work well: url are detected but not email adress... I can't create two funtions because if the first evaluation is ok, the second is useless.

Thanks for any advices. Gromit

Upvotes: 0

Views: 1529

Answers (1)

Gromit
Gromit

Reputation: 1

So, for anyone interested in implementing this kind of filter (url and email address in textarea in a Contact Form7 form - wordpress):

function custom_textarea_validation_filter($result, $tag) {  
$type = $tag['type'];
$name = $tag['name'];
if($name == 'yourmessage') {
$value = $_POST[$name];
$Match_all = "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]|[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,8}/";
if(preg_match($Match_all,$value)){

    $result->invalidate( $tag, "Merci de ne pas inclure de liens, url, ou adresse email !" );
                }
}
return $result;
}
add_filter('wpcf7_validate_textarea','custom_textarea_validation_filter', 10, 2);
add_filter('wpcf7_validate_textarea*', 'custom_textarea_validation_filter', 10, 2);

Thanks to "The fourth bird" for having helped me !

Gromit

Upvotes: 0

Related Questions