问题描述
我是 wordpress 和 php 的新手,我在我的 wordpress 网站上使用了联系表 7.因为我需要验证电子邮件地址,阻止所有免费域,例如 gmail、yahoo 等,我需要使用国家/地区代码验证印度电话号码.
I am new to wordpress and php, I'm using contact form 7 in my wordpress website. In that I need to validate the email address, block all free domains like gmail, yahoo,etc., I need to validate Indian phone number with country code.
我有 4 种类型的联系表单,但我只需要对一种表单进行此自定义验证.我用谷歌搜索并找到了 this 但它是不工作.有人请帮我解决这个问题.
I had 4 types of contact form but I need this custom validation for only one form. I googled and found this but it is not working. Someone please help me with this issue.
谢谢.
推荐答案
将以下代码添加到您的主题的functions.php
文件中.
Add following code to your theme's functions.php
file.
// Add custom validation for CF7 form fields
function is_company_email($email){ // Check against list of common public email providers & return true if the email provided *doesn't* match one of them
if(
preg_match('/@gmail.com/i', $email) ||
preg_match('/@hotmail.com/i', $email) ||
preg_match('/@live.com/i', $email) ||
preg_match('/@msn.com/i', $email) ||
preg_match('/@aol.com/i', $email) ||
preg_match('/@yahoo.com/i', $email) ||
preg_match('/@inbox.com/i', $email) ||
preg_match('/@gmx.com/i', $email) ||
preg_match('/@me.com/i', $email)
){
return false; // It's a publicly available email address
}else{
return true; // It's probably a company email address
}
}
function your_validation_filter_func($result,$tag){
$type = $tag['type'];
$name = $tag['name'];
if('yourid' == $type){ // Only apply to fields with the form field name of "company-email"
$the_value = $_POST[$name];
if(!is_company_email($the_value)){ // Isn't a company email address (it matched the list of free email providers)
$result['valid'] = false;
$result['reason'][$name] = 'You need to provide an email address that isn\'t hosted by a free provider.<br />Please contact us directly if this isn\'t possible.';
}
}
return $result;
}
add_filter( 'wpcf7_validate_text', 'your_validation_filter_func', 10, 2 ); // Email field or contact number field
add_filter( 'wpcf7_validate_text*', 'your_validation_filter_func', 10, 2 ); // Req. Email field or contact number
你可以通过上面的代码实现你想要的结果.
You can achieve your desired result by the above code.
注意:我只验证了电子邮件.您可以像我对电子邮件所做的一样对联系人执行相同的操作.
NOTE: I have validated only Email.You can do same for contact like I did for Email.
回答第二个问题:
现在,正如您所提到的,您只希望它用于一种形式,那么您可以执行以下操作:
Now as you have mentioned that you want it for only one form then you can do something like this:
wpcf7_add_shortcode( 'yourid', 'wpcf7_text_shortcode_handler', true );
然后,在表单中使用这样的标签:
Then, use a tag like this inside the form:
[yourid your-id-number-field]
如果您想了解标记语法,请浏览此页面.
If you want to understand the tag syntax then go through this page.
希望对你有帮助.
这篇关于自定义验证以在 wordpress 中的联系表格 7 中检查企业电子邮件和电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!