Contact form 7 telephone number verification

2019-07-18 20:08发布

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.

7条回答
可以哭但决不认输i
2楼-- · 2019-07-18 20:49

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);
查看更多
登录 后发表回答