2014-04-04 148 views
2

我只需要验证一个字段(称为“实例”)接受小写ASCII字母和数字,第一个字符也必须是字母而不是数字。它将接受大写字符,但我们将需要它在输入时将它们小写。因此,如果有人使用实例名称McDonalds,它将被小写为mcdonalds(而不仅仅是CSS)。空间也是不允许的。联系表7 - 自定义验证

CF7可能吗?如果是这样,请解释如何。

我已经试过this自定义验证方法,但即使使用文件中的预设自定义验证,它只是显示字段简码而非字段本身。

感谢

+0

我已经解决了这个问题,所以请查看这里的解决方案:http://stackoverflow.com/questions/36742460/email-validation-issue -in-contact-form-7-plugin-on-wordpress-resolved –

回答

0

请使用WordPress插件

jQuery验证对于联系表7 https://wordpress.org/plugins/jquery-validation-for-contact-form-7/

+0

看起来像一个很好的链接,但是你能解释一些吗? –

+0

Aaron,在安装Contact Form 7插件后安装此插件,将该字段设置为必填字段,并在“类(可选)字段”中添加类似url/number/creditcard/date的字段。使用下面的链接了解更多信息:https://wordpress.org/plugins/jquery-validation-for-contact-form-7/screenshots/ – formygalaxy

0

我也有类似的问题,用于验证名称字段,添加以下代码在我的functions.php ,你可以通过更改正则表达式来定制它

function my_wpcf7_validate_text($result, $tag) { 

    $type = $tag['type']; 
    $name = $tag['name']; 
    $value = $_POST[$name] ; 

    if (strpos($name , 'name') !== false){ 
     $regex = '/^[a-zA-Z]+$/'; 
     $Valid = preg_match($regex, $value, $matches); 
     if ($Valid > 0) { 
     } else { 
      $result->invalidate($tag, wpcf7_get_message('invalid_name')); 
     } 
    } 
    return $result; 
} 
add_filter('wpcf7_validate_text*', 'my_wpcf7_validate_text' , 10, 2); 

add_filter('wpcf7_messages', 'mywpcf7_text_messages'); 
function mywpcf7_text_messages($messages) { 
    return array_merge($messages, array(
     'invalid_name' => array(
      'description' => __("Name is invalid", 'contact-form-7'), 
      'default' => __('Name seems invalid.', 'contact-form-7') 
     ) 
    )); 
} 
+0

1.将此代码放入function.php中? 2.如何确定此代码将在我的字段中工作只说文本框? 3.我应该在管理面板中做些什么改变?4.我需要添加任何额外的属性,通过管理员联系我们形式?没有关于以上几点的解释。你只是粘贴了代码,就是这样! –

4

contactform7.com自定义验证→验证作为过滤

在触点形式7,用户输入验证被实现为过滤器 功能。用于验证的过滤器挂钩取决于 form-tag的类型,并被确定为:wpcf7_validate_ + { 类型的form-tag}。因此,对于文本表单标签,使用了过滤器钩子 wpcf7_validate_text。同样,对于电子邮件*表单标签,wpcf7_validate_email *使用 。

比方说,你在表单中的以下电子邮件字段:

Email:   [email* your-email] 
    Confirm email: [email* your-email-confirm] 

下面列出了用于验证两个领域 是否具有相同的值的代码。

add_filter('wpcf7_validate_email*', 'custom_email_confirmation_validation_filter', 20, 2); 

function custom_email_confirmation_validation_filter($result, $tag) { 
    $tag = new WPCF7_Shortcode($tag); 

    if ('your-email-confirm' == $tag->name) { 
     $your_email = isset($_POST['your-email']) ? trim($_POST['your-email']) : ''; 
     $your_email_confirm = isset($_POST['your-email-confirm']) ? trim($_POST['your-email-confirm']) : ''; 

     if ($your_email != $your_email_confirm) { 
      $result->invalidate($tag, "Are you sure this is the correct address?"); 
     } 
    } 
    return $result; 
} 

将两个参数传递给过滤器函数:$ result和 $ tag。 $ result是WPCF7_Validation类的一个实例,用于管理验证过程的一个 序列。 $ tag是由给定的form-tag组件组成的关联数组 ;正如您在前面的 配方中看到的那样,您可以使用WPCF7_Shortcode类来处理这种类型的数据。

查看过滤器功能的内部。首先,检查窗体标签的名称 ,以确保验证仅适用于 特定字段(your-email-confirm)。

然后比较两个电子邮件字段值,如果它们不匹配,则会调用 $ result-> invalidate()。您需要将两个参数 传递给invalidate()方法:第一个参数应该是$ tag 变量,第二个参数是验证错误消息 ,您希望该字段显示。

最后,不要忘记返回$结果。

+2

这是从插件博客复制并粘贴的内容:http://contactform7.com/2015/03/28/custom-validation/ – qbeauperin

+2

对于googlers:'WPCF7_Shortcode'已被弃用。你应该改用'WPCF7_FormTag'。请参阅https://contactform7.com/2016/12/03/contact-form-7-46/ –

0

//添加自定义的验证了CF7表单字段

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' == $value){ // 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->invalidate($tag, wpcf7_get_message('invalid_email')); 
       } 
     } 
     return $result; 
} 

add_filter('wpcf7_validate_email', 'your_validation_filter_func', 10, 2); 

// Email field or contact number field 
    add_filter('wpcf7_validate_email*', 'your_validation_filter_func', 10, 2);  // Req. Email field or contact number 
+0

我已编辑上面张贴的Zafar S的代码。对代码进行的更改很少,并且记得要更改密码 – Raks