可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am trying to use the built in verification method for contact form 7 to verify a telephone number.
Contact form 7 markup:
<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.
So, how can I set the min and max value to support all possible 10 digit telephone numbers?
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.
I also tried to edit the plugin files via functions.php file by using the code from a source but that did not work.
So, if any one has the perfect solution to validate telephone numbers on contact form 7 then please post your answers.
回答1:
Try this one definitely it will work as per contact form 7 documentation.
[number* your-telephone minlength:10 maxlength:140 placeholder "Telephone number"]
回答2:
You can use [tel* tel-672] field and validate according to your requirement through wpcf7_is_tel filter hook .
Contact form 7 has 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
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
// 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
回答3:
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.
Filter: https://plugins.trac.wordpress.org/browser/contact-form-7/trunk/modules/text.php wpcf7_validate_tel
which is using the function wpcf7_validate_tel( $result, $tag )
回答4:
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
$atts['maxlength'] = $tag->get_maxlength_option();
$atts['minlength'] = $tag->get_minlength_option();
(in wpcf7_text_form_tag_handler function)... and validation filters
$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)
And now paste it to the numbers.php in the right place.
So in wpcf7_number_form_tag_handler function atts looks like:
$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:
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;
回答5:
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.
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);
回答6:
Try this:
[tel your-phone minlength:10 maxlength:140]
good luck!!
chucha!
回答7:
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]