php 联系表 7 电话号码验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35425854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Contact form 7 telephone number verification
提问by Kiran Dash
I am trying to use the built in verification method for contact form 7 to verify a telephone number.
我正在尝试使用联系表 7 的内置验证方法来验证电话号码。
Contact form 7 markup:
联系表格 7 标记:
<p>[number* your-telephone min:1000000000 max:9999999999 placeholder "Telephone number"] </p>
<p>[submit "submit"]</p>
So, what I am trying to do here is restrict phone number using the min and max properties of input type of number. But the problem here is that if I enter a telephone number say: 0402356584 then it is less than the min value but is still a phone number.
所以,我在这里尝试做的是使用输入类型数字的 min 和 max 属性来限制电话号码。但这里的问题是,如果我输入一个电话号码,比如:0402356584,那么它小于最小值,但仍然是一个电话号码。
So, how can I set the min and max value to support all possible 10 digit telephone numbers?
那么,如何设置最小值和最大值以支持所有可能的 10 位电话号码?
Any other solution different from my approach is also most welcome. Because I have got a feeling that the verification can not be done using min and max attributes.
与我的方法不同的任何其他解决方案也是最受欢迎的。因为我有一种感觉,无法使用 min 和 max 属性进行验证。
I also tried to edit the plugin files via functions.php file by using the code from a sourcebut that did not work.
我还尝试使用源代码通过functions.php文件编辑插件文件,但没有用。
So, if any one has the perfect solution to validate telephone numbers on contact form 7 then please post your answers.
因此,如果有人有完美的解决方案来验证联系表 7 上的电话号码,请发布您的答案。
回答by shishir mishra
You can use [tel* tel-672]field and validate according to your requirement through wpcf7_is_telfilter hook .
您可以使用[tel* tel-672]字段并根据您的要求通过 wpcf7_is_telfilter hook 进行验证。
Contact form 7has many pre-defined hooks through which you can validate any field.Here, in the Contact Form 7 formatting.php module, validation rule is defined by following method .You can override it by filter hook mentioned in apply_filters through functions.php
Contact form 7有很多预定义的钩子,你可以通过这些钩子验证任何字段。这里,在Contact Form 7 format.php 模块中,验证规则是通过以下方法定义的。您可以通过函数apply_filters 中提到的过滤器钩子覆盖它。 php
function wpcf7_is_tel( $tel ) {
$result = preg_match( '/^[+]?[0-9() -]*$/', $tel );
return apply_filters( 'wpcf7_is_tel', $result, $tel );
}
Please add the below mentioned code in functions.php in your activated theme folder and add your validation rule to $result
请在您激活的主题文件夹中的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 );
You can see more
你可以看到更多
回答by VaqasUddin
Try this one definitely it will work as per contact form 7 documentation.
试试这个肯定会按照联系表格 7 文档工作。
[number* your-telephone minlength:10 maxlength:140 placeholder "Telephone number"]
回答by Talha Mughal
In my case, below code is working. Important point is that input type should be 'tel' as below.
就我而言,下面的代码正在运行。重要的一点是输入类型应该是“电话”,如下所示。
[tel your-telephone minlength:1 maxlength:10]
回答by TomLi
I Think i have nice workaround...
我想我有很好的解决方法......
By the default the number field dont have minlength and maxlength validators. You can copy them from wp-content/plugins/contact-form-7/modules/text.php
默认情况下,数字字段没有 minlength 和 maxlength 验证器。 您可以从 wp-content/plugins/contact-form-7/modules/text.php 复制它们
$atts['maxlength'] = $tag->get_maxlength_option();
$atts['minlength'] = $tag->get_minlength_option();
(in wpcf7_text_form_tag_handler function)... and validation filters
(在 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' ) );
}
(in wpcf7_text_validation_filter function)
(在 wpcf7_text_validation_filter 函数中)
And now paste it to the numbers.php in the right place.So in wpcf7_number_form_tag_handler function atts looks like:
现在将其粘贴到 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();
and wpcf7_number_validation_filter function ends with:
和 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;
回答by Ananthaselvam P
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);
phonenumber -> field name
电话号码 -> 字段名称
try this one for function.php file.
试试这个 function.php 文件。
Filter: https://plugins.trac.wordpress.org/browser/contact-form-7/trunk/modules/text.phpwpcf7_validate_tel
which is using the function wpcf7_validate_tel( $result, $tag )
过滤器:https: //plugins.trac.wordpress.org/browser/contact-form-7/trunk/modules/text.phpwpcf7_validate_tel
正在使用该功能wpcf7_validate_tel( $result, $tag )
回答by Ben
This works for tel type fields. If you want to use number or text fields, you'll need to change 'wpcf7_validate_tel' in the filter param as well as the code.
这适用于电话类型字段。如果要使用数字或文本字段,则需要更改过滤器参数和代码中的“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);
回答by ValRob
Try this:
尝试这个:
[tel your-phone minlength:10 maxlength:140]
good luck!!
祝你好运!!
chucha!
呸!