我只需要验证一个字段(称为“实例”)以仅接受小写 ASCII 字母和数字,第一个字符也必须是字母而不是数字。它会接受大写字符,但我们需要它在输入时将它们小写。因此,如果有人使用实例名称 McDonalds,它将小写为 mcdonalds(不仅仅是 CSS)。也不允许有空格。
CF7 可以实现这一点吗?如果是这样,请解释一下如何。
我已经尝试过this自定义验证方法,但即使使用文件中预设的自定义验证,它也只是显示字段短代码而不是字段本身。
谢谢
来自 contactform7.com 自定义验证 → 作为过滤器验证:
在联系表单 7 中,用户输入验证是作为过滤器实现的 功能。用于验证的过滤器挂钩因不同而异 form-tag 的类型,确定为: wpcf7_validate_ + {type of 表单标签}。因此,对于文本表单标签,过滤器钩子 使用 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_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
看一下过滤器功能的内部。首先,检查名称 表单标签以确保验证仅应用于 特定领域 (
).your-email-confirm
然后比较两个电子邮件字段值,如果不匹配,
将被调用。您需要传递两个参数 对于$result->invalidate()
方法:第一个参数应该是invalidate()
变量,第二个参数是验证错误信息 您希望该字段显示的内容。$tag
最后,别忘了归还
。$result
// 为 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
我在验证名称字段时遇到了类似的问题,我在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' )
)
));
}
请使用这个wordpress插件
联系表 7 的 Jquery 验证 https://wordpress.org/plugins/jquery-validation-for-contact-form-7/
您可以使用
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
方法在强制转换之前不起作用。
试试这个插件。免费版本允许为每个字段设置自定义验证消息。 网址:https://wordpress.org/plugins/cf7-custom-validation-message/
var cf = jQuery("#contact_form").validate({
rules: {
'your-fname': {
required: true
},
'your-lname': {
required: true
},
'your-email': {
required: true,
email: true
},
'your-phone': {
required: true,
//phoneAu: true,
rangelength: [5, 16]
},
'your-subject': {
required: true
}
},
messages: {
'your-fname': {
required: "Please enter your first name."
},
'your-lname': {
required: "Please enter your last name."
},
'your-email': {
required: "Please enter your email address.",
email: "Please enter valid email address."
},
'your-phone': {
required: "Please enter your phone number.",
rangelength: "Please enter valid phone number."
},
'your-subject': {
required: "Please enter your subject."
}
},
errorPlacement: function (error, element) {
error.insertAfter(element);
},
errorElement: "label",
errorClass: "error"
});