联系表格 7 电话号码验证

Contact form 7 telephone number verification

我正在尝试使用联系表 7 的内置验证方法来验证电话phone号码。

联系表 7 标记:

<p>[number* your-telephone min:1000000000 max:9999999999 placeholder "Telephone number"] </p>

<p>[submit "submit"]</p>

因此,我在这里尝试做的是使用数字输入类型的最小和最大属性限制 phone 数字。但这里的问题是,如果我输入一个电话 phone 号码说:0402356584 那么它小于最小值但仍然是一个 phone 号码。

那么,如何设置最小值和最大值以支持所有可能的 10 位电话 phone 号码?

也非常欢迎与我的方法不同的任何其他解决方案。因为我有一种感觉,使用min和max属性无法完成验证。

我还尝试使用 source 中的代码通过 functions.php 文件编辑插件文件,但这没有用。

因此,如果有人有完美的解决方案来验证联系表 7 上的电话phone 号码,那么请post 您的回答。

试试这个,它肯定会按照 contact form 7 documentation.

工作
[number* your-telephone minlength:10 maxlength:140 placeholder "Telephone number"] 

    function custom_phone_validation($result,$tag){
        $type = $tag['type'];
        $name = $tag['name'];
        if($name == 'phonenumber'){
                    $phoneNumber = isset( $_POST['phonenumber'] ) ? trim( $_POST['phonenumber'] ) : '';
            $the_value = preg_match("/your_reg_exp format for phone number/",$_POST[$name]);
            if($phoneNumber == "" || $the_value == false ){ 
                $result->invalidate( $tag, "please enter vaild phone number" );
            }
        }
        return $result;
    }
    add_filter('wpcf7_validate_tel','custom_phone_validation', 10, 2);
    add_filter('wpcf7_validate_tel*', 'custom_phone_validation', 10, 2);

电话号码 -> 字段名称

为 function.php 个文件尝试这个。

过滤器:https://plugins.trac.wordpress.org/browser/contact-form-7/trunk/modules/text.php wpcf7_validate_tel 正在使用函数 wpcf7_validate_tel( $result, $tag )

您可以使用 [tel* tel-672] 字段并根据您的要求通过 wpcf7_is_tel 过滤器钩子进行验证.

Contact form 7 有许多 pre-defined 挂钩,通过它们您可以在 Contact Form 7 formatting.php 模块中验证任何 field.Here,验证规则由以下方法定义。您可以通过 apply_filters 到 functions.php

中提到的过滤器挂钩来覆盖它
function wpcf7_is_tel( $tel ) {
    $result = preg_match( '/^[+]?[0-9() -]*$/', $tel );
    return apply_filters( 'wpcf7_is_tel', $result, $tel );
}

请在您激活的主题文件夹中的 functions.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 );

可以看到more

这适用于电话类型字段。如果您想使用数字或文本字段,则需要在过滤器参数和代码中更改 '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);

试试这个:

[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 函数中)

然后粘贴到 numbers.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;

就我而言,以下代码有效。 重要的一点是输入类型应该是 'tel' 如下。

[tel your-telephone minlength:1 maxlength:10]

根据其他解决方案,最小长度为 10 是首选,以确保访问者在提交时包含区号。我还允许使用 15 个字符,以防访问者包含国家代码和破折号“+1 555-555-5555”

[tel* your-phone minlength:10 maxlength:15]