2014-06-05 22 views
0

如何設置聯繫人表格7插件的文本字段以僅允許某些字符? 我希望這個字段只接受被認爲是特殊字符的希臘字符。 有沒有辦法爲此添加24個字符?聯繫表格7只接受某些字符

+0

'聯繫表格7'與正則表達式有什麼關係? – sln

回答

0

這很可能是可能的,因爲其他人已經表明可以定製它。

先註冊你的回調方法:

//add fiter for text area validation 
add_filter('wpcf7_validate_textarea', 'cf7_custom_textarea_validation', 10, 2); 
add_filter('wpcf7_validate_textarea*', 'cf7_custom_textarea_validation', 10, 2); 

而且回調裏面,做您的驗證:

function cf7_custom_form_validation($result, $tag) 
{ 
    $type = $tag['type']; 
    $name = $tag['name']; 
    $value = $_POST[$name]; 

    ... 

    if ($type == 'yourtype*' && !preg_match($your_regex, $value)) { 
     $result['valid'] = false; 
     $result['reason'][$name] = __('message for your regex not matching'); 
    } 

    ... 

看到這裏exemplare代碼:

這裏閱讀示範性的博文:

聯繫從7的支持,請訪問:http://wordpress.org/support/plugin/contact-form-7

你找到http://codex.wordpress.org/記錄所有的WordPress API函數。

你找到http://php.net/docs

0

聯繫表7的版本4.5.1 一切 PHP相關的記錄。它爲我工作。

//code to validate textbox 
function custom_text_validation_filter($result, $tag) { 
     $type = $tag['type']; 
     $name = $tag['name']; 
     //here textbox type name is 'subject' 
     if($name == 'subject') { 
      $value = $_POST[$name]; 
      if (preg_match('/[\'^£$%&*()}{@#~><>|=_+¬]/', $value)){ 
      $result->invalidate($tag, "Invalid characters."); 
      } 
     } 
     return $result; 
} 
add_filter('wpcf7_validate_text','custom_text_validation_filter', 10, 2); 
add_filter('wpcf7_validate_text*', 'custom_text_validation_filter', 10, 2); 

//code to validate textarea 
function custom_textarea_validation_filter($result, $tag) { 
     $type = $tag['type']; 
     $name = $tag['name']; 
     //here textarea type name is 'message' 
     if($name == 'message') { 
      $value = $_POST[$name]; 
      if (preg_match('/[\'^£$%&*()}{@#~><>|=_+¬]/', $value)){ 
      $result->invalidate($tag, "Invalid characters."); 
      } 
     } 
     return $result; 
} 
add_filter('wpcf7_validate_textarea','custom_textarea_validation_filter', 10, 2); 
add_filter('wpcf7_validate_textarea*', 'custom_textarea_validation_filter', 10, 2);