[英]Contact Form 7 - Custom Validation
我只需要驗證一個字段(稱為“實例”)以僅接受小寫 ASCII 字母和數字,第一個字符也必須是字母而不是數字。 它將接受大寫字符,但我們需要它在輸入時將它們小寫。 因此,如果有人使用實例名稱 McDonalds,它將被小寫為 mcdonalds(不僅僅是使用 CSS)。 也不允許有空格。
這可以用 CF7 實現嗎? 如果是這樣,請解釋如何。
我已經嘗試過這種自定義驗證方法,但即使在文件中使用預設的自定義驗證,它也只是顯示字段短代碼而不是字段本身。
謝謝
從contactform7.com上的Custom Validation → Validation as a Filter :
在 Contact Form 7 中,用戶輸入驗證是作為過濾器功能實現的。 用於驗證的過濾器鈎子取決於表單標簽的類型,確定為:wpcf7_validate_ + {表單標簽的類型}。 因此,對於文本表單標簽,使用過濾器鈎子 wpcf7_validate_text。 同樣, wpcf7_validate_email* 用於 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_FormTag($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
是由給定的表單標簽組件組成的關聯數組; 正如您在上一個WPCF7_FormTag
,您可以使用WPCF7_FormTag
類來處理此類數據。查看過濾器功能的內部。 首先,檢查表單標簽的名稱以確保驗證僅應用於特定字段 (
your-email-confirm
)。然后比較兩個電子郵件字段值,如果它們不匹配,將調用
$result->invalidate()
。 您需要向invalidate()
方法傳遞兩個參數:第一個參數應該是$tag
變量,第二個參數是您希望該字段顯示的驗證錯誤消息。最后,不要忘記返回
$result
。
我在驗證名稱字段時遇到了類似的問題,我在我的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' )
)
));
}
// 為 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
您可以使用add_filter
函數為表單字段輸入添加您自己的自定義驗證。
要為textarea
字段添加自定義驗證,您可以在主題的根目錄中的functions.php
文件中添加以下內容。
add_filter( 'wpcf7_validate_textarea*', 'custom_textarea_validation_filter', 1, 2 );
function custom_textarea_validation_filter( $result, $tag ) {
$tag = new WPCF7_Shortcode($tag);
$result = (object)$result;
$name = 'project-synopsis';
if ( $name == $tag->name ) {
$project_synopsis = isset( $_POST[$name] ) ? trim( wp_unslash( (string) $_POST[$name] ) ) : '';
if ( empty( $project_synopsis ) ) {
$result->invalidate( $tag, "Please write a quick project synopsis." );
}
}
return $result;
}
對我來說,訣竅是將$result
參數轉換為對象,因為用於添加錯誤消息的invalidate
方法在轉換之前不起作用。
請使用這個wordpress插件
聯系表格 7 的 Jquery 驗證https://wordpress.org/plugins/jquery-validation-for-contact-form-7/
試試這個插件。 允許在免費版本中為每個字段設置自定義驗證消息。 網址: https : //wordpress.org/plugins/cf7-custom-validation-message/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.