【问题标题】:Contact form 7 telephone number verification联系表7电话号码验证
【发布时间】:2016-05-27 08:04:23
【问题描述】:

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

联系表格 7 标记:

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

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

所以,我在这里要做的是使用数字输入类型的 min 和 max 属性来限制电话号码。但是这里的问题是,如果我输入一个电话号码说:0402356584 那么它小于最小值但仍然是一个电话号码。

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

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

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

所以,如果有人有完美的解决方案来验证联系表 7 上的电话号码,那么请发布您的答案。

【问题讨论】:

    标签: php wordpress forms contact-form-7


    【解决方案1】:

    试试这个肯定会按照contact form 7 documentation工作。

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

    【讨论】:

    • 感谢您的回答。让我想到可以使用 minlength 和 maxlength 轻松解决这个问题。请查看我的编辑,以便其他人可以发现您的回答有用。
    • 为什么 maxlength 是 140?
    • 为什么要编号?可能是[tel your-phone minlength:10 maxlength:140]
    【解决方案2】:

    您可以使用 [tel* tel-672] 字段并根据您的要求通过 wpcf7_is_tel filter hook 进行验证。

    Contact form 7 有许多预定义的钩子,您可以通过这些钩子验证任何字段。这里,在 Contact Form 7formatting.php 模块中,验证规则由以下方法定义。您可以覆盖它通过functions.php的apply_filters中提到的过滤器钩子

    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

    【讨论】:

    • 我想限制用户只能添加123-123-1234 格式,而不是'1234567890' 或其他,我可以这样做吗?上面的钩子可以吗
    • 是上面的钩子将用于验证联系表格的电话号码类型 7.您只需替换此格式的正则表达式“123-123-1234”
    【解决方案3】:

    就我而言,下面的代码正在运行。 重要的一点是输入类型应该是'tel',如下所示。

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:
        函数 custom_phone_validation($result,$tag){ $type = $tag['type']; $name = $tag['name']; if($name == '电话号码'){ $phoneNumber = isset($_POST['phonenumber']) ?修剪($_POST['phonenumber']):''; $the_value = preg_match("/your_reg_exp 电话号码格式/",$_POST[$name]); if($phoneNumber == "" || $the_value == false ){ $result->invalidate( $tag, "请输入有效的电话号码" ); } } 返回$结果; } 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 类型,请将过滤器参数从 wpcf7_validate_text 更改为 wpcf7_validate_tel
        • 我应该在 /your_reg_exp 格式的电话号码 / 中输入什么,并且我想将其最小字符设置为至少 10?
        【解决方案6】:

        试试这个:

        [tel your-phone minlength:10 maxlength:140]
        

        祝你好运!

        楚查!

        【讨论】:

        • 你能分享一下这背后的登录信息吗?
        【解决方案7】:

        我认为我有很好的解决方法...

        默认情况下,数字字段没有 minlength 和 maxlength 验证器。 您可以从 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;
        

        【讨论】:

          【解决方案8】:

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

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-06-06
            • 2015-09-05
            • 1970-01-01
            • 2012-10-27
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多