验证带有2个条件的文本区域 联系表格 7

问题描述 投票:0回答:1

我想对CF7表单中的一个文本框执行两个测试。

第一:textarea没有包含url的内容

第二:同一个文本区域不包含电子邮件地址。

我试过这段代码。

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是我要测试的文本区域。

但它工作得不好:检测到url,但没有检测到电子邮件地址......我不能创建两个函数,因为如果第一个评估是确定的,第二个是没用的。

谢谢你的任何建议.Gromit

php regex wordpress validation contact-form-7
1个回答
0
投票

所以,对于任何有兴趣实现这种过滤的人来说(在联系表格7中的文本框中的url和email地址 - 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);

感谢 "第四只鸟 "对我的帮助!

格罗米特

© www.soinside.com 2019 - 2024. All rights reserved.