How to validate contact form 7 telephone number in WordPress
As we know that the contact form 7 is the most used plugin in wordpress for creating contact forms and sending emails etc.In wordpress, plugins are developed in such way using the wordpress hooks concept “action and filter” through which someone can easily customize any plugin without modifying core files of plugin.
So, contact form 7 has already added validation on different types of fields in formatting.php file in which validation rules are defined.Here, we need to alter the validation of telephone field which is already applied in below mentioned function and we can override it using filter hook “add_filter” because “apply_filters” is used to return the validation result.
1 2 3 4 |
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
1 2 3 4 5 6 7 |
// 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 ); |
thanks shishir,
but without this coding, any other alternative for solving this validation.
my client is based in dubai and there is 11 digital no.
how to do that..
Thanks, it’s really helpful one suggestion for my side your code is working fine but please explain more to people after adding this code you would get this result.
cheers!