自定义验证,以检查wordpress中联系表格7中的业务电子邮件和电话号码

Gop*_*mal 5 email validation wordpress contact-form-7

我是wordpress和php的新手,我在wordpress网站上使用联系表格7.在那里我需要验证电子邮件地址,阻止所有免费域名,如gmail,yahoo等,我需要用国家代码验证印度电话号码.

我有4种类型的联系表单,但我只需要一个表单的自定义验证.我用Google搜索并找到了这个,但它无效.有人请帮我解决这个问题.

谢谢.

Roh*_*ner 5

添加以下代码到你的主题functions.php文件.

    // 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
Run Code Online (Sandbox Code Playgroud)

您可以通过上面的代码实现所需的结果.

注意:我只验证了电子邮件.您可以像我为电子邮件那样进行联系.

回答第二个问题:

现在正如您所提到的那样,您只需要一个表单,那么您可以执行以下操作:

wpcf7_add_shortcode( 'yourid', 'wpcf7_text_shortcode_handler', true );
Run Code Online (Sandbox Code Playgroud)

然后,在表单中使用这样的标记:

[yourid your-id-number-field] 
Run Code Online (Sandbox Code Playgroud)

如果您想了解标签语法,请浏览此页面.

希望它能帮到你.