Contact form 7 telephone number verification

73,345

Solution 1

Try this one definitely it will work as per contact form 7 documentation.

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

Solution 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

Solution 3

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]

Solution 4

Try this:

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

good luck!!

chucha!

Solution 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);
Share:
73,345
Kiran Dash
Author by

Kiran Dash

On weekends, I am a tutor and a blogger creating Web development tutorials for my youtube channel BG Web Agency and blog. Film making is another hobby of mine. Want yourself entertained? Have a look at my short films channel: BG Films

Updated on August 13, 2021

Comments

  • Kiran Dash
    Kiran Dash over 2 years

    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.

  • Kiran Dash
    Kiran Dash about 8 years
    Thanks for your answer. Gave me the idea that minlength and maxlength can be used to solve the case with ease. Please review my edit so that other people can find your answer useful.
  • Developer
    Developer almost 7 years
    i want to restrict user to only add 123-123-1234 Format, Not '1234567890' or others, can i do this.? is ti possible by above hook
  • shishir mishra
    shishir mishra over 6 years
    Yes above hook will be used to validate phone number type of contact form 7.You have to just replace regular expression for this format "123-123-1234"
  • Ben
    Ben about 6 years
    Why is maxlength 140?
  • Ben
    Ben about 6 years
    Beware, if you are using a tel type in your form, change the filter params from wpcf7_validate_text to wpcf7_validate_tel
  • ValRob
    ValRob almost 6 years
    why number? it could be [tel your-phone minlength:10 maxlength:140]
  • iceiceicy
    iceiceicy over 5 years
    What should I put inside /your_reg_exp format for phone number/ and also I want to make set it minimum character to at least 10?
  • Srinivas08
    Srinivas08 over 3 years
    Can you pls share login behind this ?