2014-04-04 59 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