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 by minlength and maxlength option
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 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 functions.php file. The filter hook is located in contact-form-7/includes/submission.php file. The hook is wpcf7_validate_tel.
More details https://doactions.org/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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.