联系表格7电话号码验证

Kir*_*ash 3 php forms wordpress contact-form-7

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

联系表格7加价:

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

<p>[submit "submit"]</p>
Run Code Online (Sandbox Code Playgroud)

所以,我在这里尝试做的是使用输入类型的数字的最小和最大属性来限制电话号码.但问题是如果我输入一个电话号码:0402356584然后它小于最小值但仍然是电话号码.

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

任何其他与我的方法不同的解决方案也是最受欢迎的.因为我觉得使用min和max属性无法进行验证.

我还试图通过使用来自代码的代码通过functions.php文件编辑插件文件,但这不起作用.

因此,如果任何人有完整的解决方案来验证联系表格7上的电话号码,那么请发布您的答案.

shi*_*hra 9

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

联系表单7有许多预定义的挂钩,您可以通过它来验证任何字段.在Contact Form 7 formatting.php模块中,验证规则由以下方法定义.您可以通过函数中的apply_filters中提到的过滤器挂钩来覆盖它. PHP

function wpcf7_is_tel( $tel ) {
    $result = preg_match( '/^[+]?[0-9() -]*$/', $tel );
    return apply_filters( 'wpcf7_is_tel', $result, $tel );
}
Run Code Online (Sandbox Code Playgroud)

请在您激活的主题文件夹中的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 );
Run Code Online (Sandbox Code Playgroud)

你可以看到更多


小智 8

尝试这个肯定它将按照联系表格7文档工作.

[number* your-telephone minlength:10 maxlength:140 placeholder "Telephone number"] 
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的回答.给我的想法是minlength和maxlength可以用来轻松解决问题.请查看我的编辑,以便其他人可以找到您的答案有用. (2认同)
  • 为什么maxlength 140? (2认同)
  • 为什么是数字?它可能是`[tel your-phone minlength:10 maxlength:140]` (2认同)

小智 5

就我而言,下面的代码有效。重要的一点是输入类型应为“tel”,如下所示。

[tel your-telephone minlength:1 maxlength:10]
Run Code Online (Sandbox Code Playgroud)