The Contact Form 7 phone number validation by shortcode or custom PHP scripts. Allowed tel shortcode options are minlength, maxlength, size, etc.
CF7 Phone Number Validation By Shortcode Options
Validate the number of digits using the minlength and maxlength options
Example:-
[tel* your-phone minlength:10 maxlength:11]
Contact Form 7 Fixed Digit Phone Number Validation
You can validate a digit phone number by adding minlength and maxlength options in the tel shortcode.
Example:-
Indian 10-digit phone number validation
[tel* your-phone minlength:10 maxlength:10]
CF7 Phone Number Validation By PHP Scripts
Add the following code to your selected theme’s functions.php file. The filter hook is located in the contact-form-7/includes/submission.php file. The hook is 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);
Contact Form 7 Phone Number With Country Code
Enable international telephone input with the Advanced Telephone Input plugin.