我正在尝试使用联系表格 7 的内置验证方法来验证电话号码。
联系表格 7 标记:
<p>[number* your-telephone min:1000000000 max:9999999999 placeholder "Telephone number"] </p>
<p>[submit "submit"]</p>
因此,我在这里要做的是使用数字输入类型的最小和最大属性来限制电话号码。但这里的问题是,如果我输入一个电话号码说:0402356584那么它小于最小值,但仍然是一个电话号码。
那么,如何设置最小值和最大值以支持所有可能的 10 位电话号码?
与我的方法不同的任何其他解决方案也是最受欢迎的。因为我有一种感觉,使用最小和最大属性无法完成验证。
我还尝试使用源代码中的代码通过函数.php文件编辑插件文件,但这不起作用。
因此,如果有人有完美的解决方案来验证联系表格 7 上的电话号码,那么请发布您的答案。
试试这个,它肯定会按照联系表格 7 文档工作。
[number* your-telephone minlength:10 maxlength:140 placeholder "Telephone number"]
您可以使用 [tel* tel-672] 字段并通过过滤器挂钩根据您的要求进行验证wpcf7_is_tel 。
联系表单 7 有许多预定义的钩子,您可以通过这些钩子验证任何字段。在这里,在联系人表单 7 格式.php模块中,验证规则由以下方法定义。您可以通过函数通过apply_filters中提到的过滤器钩子覆盖它.php
function wpcf7_is_tel( $tel ) {
$result = preg_match( '/^[+]?[0-9() -]*$/', $tel );
return apply_filters( 'wpcf7_is_tel', $result, $tel );
}
请在函数中添加下面提到的代码.php在您的激活的主题文件夹中,并将您的验证规则添加到$result
// define the wpcf7_is_tel callback
function custom_filter_wpcf7_is_tel( $result, $tel ) {
$result = preg_match( '/^'(?'+?([0-9]{1,4})?')?[-'. ]?('d{10})$/', $tel );
return $result;
}
add_filter( 'wpcf7_is_tel', 'custom_filter_wpcf7_is_tel', 10, 2 );
您可以看到更多
就我而言,下面的代码正在工作。重要的一点是输入类型应为"tel",如下所示。
[tel your-telephone minlength:1 maxlength:10]
适用于 tel 类型字段。如果要使用数字或文本字段,则需要更改筛选器参数中的"wpcf7_validate_tel"以及代码。
function custom_phone_validation($result,$tag){
$type = $tag->type;
$name = $tag->name;
if($type == 'tel' || $type == 'tel*'){
$phoneNumber = isset( $_POST[$name] ) ? trim( $_POST[$name] ) : '';
$phoneNumber = preg_replace('/[() .+-]/', '', $phoneNumber);
if (strlen((string)$phoneNumber) != 10) {
$result->invalidate( $tag, 'Please enter a valid phone number.' );
}
}
return $result;
}
add_filter('wpcf7_validate_tel','custom_phone_validation', 10, 2);
add_filter('wpcf7_validate_tel*', 'custom_phone_validation', 10, 2);
函数custom_phone_validation($result,$tag({ $type = $tag['类型']; $name = $tag['名称']; if($name == 'phonenumber'({ $phoneNumber = isset( $_POST['电话号码'] ( ?trim( $_POST["电话号码"] ( : ''; $the_value = preg_match("/your_reg_exp 电话号码/"的格式/",$_POST[$name](; if($phoneNumber == " ||$the_value == false ({ $result->无效($tag,"请输入有效的电话号码"(; } } 返回$result; } add_filter("wpcf7_validate_tel","custom_phone_validation", 10, 2(; add_filter("wpcf7_validate_tel*", "custom_phone_validation", 10, 2(;
电话号码 ->字段名称
试试这个函数.php文件。
过滤器:https://plugins.trac.wordpress.org/browser/contact-form-7/trunk/modules/text.php wpcf7_validate_tel
正在使用函数wpcf7_validate_tel( $result, $tag )
试试这个:
[tel your-phone minlength:10 maxlength:140]
祝你好运!!
咔嚓!
我有很好的解决方法...
默认情况下,数字字段没有最小长度和最大长度验证器。您可以从wp-content/plugins/contact-form-7/modules/text复制它们.php
$atts['maxlength'] = $tag->get_maxlength_option();
$atts['minlength'] = $tag->get_minlength_option();
(在wpcf7_text_form_tag_handler函数中(...和验证筛选器
$code_units = wpcf7_count_code_units( stripslashes( $value ) );
if ( $maxlength && $maxlength < $code_units ) {
$result->invalidate( $tag, wpcf7_get_message( 'invalid_too_long' ) );
} elseif ( $minlength && $code_units < $minlength ) {
$result->invalidate( $tag, wpcf7_get_message( 'invalid_too_short' ) );
}
(在wpcf7_text_validation_filter函数中(
现在将其粘贴到数字.php正确的位置。所以在wpcf7_number_form_tag_handler函数中,atts看起来像:
$atts['class'] = $tag->get_class_option( $class );
$atts['id'] = $tag->get_id_option();
$atts['tabindex'] = $tag->get_option( 'tabindex', 'signed_int', true );
$atts['min'] = $tag->get_option( 'min', 'signed_int', true );
$atts['max'] = $tag->get_option( 'max', 'signed_int', true );
$atts['step'] = $tag->get_option( 'step', 'int', true );
$atts['maxlength'] = $tag->get_maxlength_option();
$atts['minlength'] = $tag->get_minlength_option();
wpcf7_number_validation_filter函数以:
if ( $tag->is_required() && '' == $value ) {
$result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) );
} elseif ( '' != $value && ! wpcf7_is_number( $value ) ) {
$result->invalidate( $tag, wpcf7_get_message( 'invalid_number' ) );
} elseif ( '' != $value && '' != $min && (float) $value < (float) $min ) {
$result->invalidate( $tag, wpcf7_get_message( 'number_too_small' ) );
} elseif ( '' != $value && '' != $max && (float) $max < (float) $value ) {
$result->invalidate( $tag, wpcf7_get_message( 'number_too_large' ) );
} elseif ( $maxlength && $maxlength < $code_units ) {
$result->invalidate( $tag, wpcf7_get_message( 'invalid_too_long' ) );
} elseif ( $minlength && $code_units < $minlength ) {
$result->invalidate( $tag, wpcf7_get_message( 'invalid_too_short' ) );
}
return $result;
根据其他解决方案,最好将最小长度设置为 10,以确保访问者在其提交中包含区号。我还允许使用 15 个字符,以防访问者包含国家/地区代码和破折号"+1 555-555-5555">
[tel* your-phone minlength:10 maxlength:15]