If you want to validate you phone number within contact form 7 wordpress plugin. Here is the great example of that. Please follow the give steps .
first, i went to the plugin folder and edited, contact-form-7/modules/text.php
and created a new function (please excuse my mistakes in naming conventions).
1. i inserted this code:
wpcf7_add_shortcode( ‘digit’, ‘wpcf7_text_shortcode_handler’, true );
wpcf7_add_shortcode( ‘digit*’, ‘wpcf7_text_shortcode_handler’, true );
after this:
wpcf7_add_shortcode( ‘email*’, ‘wpcf7_text_shortcode_handler’, true );
2. i inserted this code:
if ( ‘digit’ == $type || ‘digit*’ == $type )
$class_att .= ‘ wpcf7-validates-as-digit’;
after this:
if ( ‘text*’ == $type || ‘email*’ == $type )
$class_att .= ‘ wpcf7-validates-as-required’;
3. i inserted this code:
add_filter( ‘wpcf7_validate_email*’, ‘wpcf7_text_validation_filter’, 10, 2 );
add_filter( ‘wpcf7_validate_digit*’, ‘wpcf7_text_validation_filter’, 10, 2 );
after this:
add_filter( ‘wpcf7_validate_email’, ‘wpcf7_text_validation_filter’, 10, 2 );
4. i inserted this code:
if ( ‘digit’ == $type || ‘digit*’ == $type ) {
if ( ” == $_POST[$name] ) {
$result['valid'] = false;
$result['reason'][$name] = $wpcf7_contact_form->message( ‘invalid_required’ );
}
elseif ( ” != $_POST[$name] && ! is_telnum( $_POST[$name] ) ) {
$result['valid'] = false;
$result['reason'][$name] = $wpcf7_contact_form->message( ‘invalid_digit’ );
}
}
after this if code:
if ( ‘email’ == $type || ‘email*’ == $type ) {
if ( ‘email*’ == $type && ” == $_POST[$name] ) {
$result['valid'] = false;
$result['reason'][$name] = $wpcf7_contact_form->message( ‘invalid_required’ );
} elseif ( ” != $_POST[$name] && ! is_email( $_POST[$name] ) ) {
$result['valid'] = false;
$result['reason'][$name] = $wpcf7_contact_form->message( ‘invalid_email’ );
}
}
second, i went to contact-form-7/includes/functions.php to add the error message for the telephone number validation.
1. i inserted this code:
‘invalid_digit’ => array(
‘description’ => __( “There is a field that sender is needed to fill in with numbers”, ‘wpcf7′ ),
‘default’ => __( ‘Please fill the required field with numbers.’, ‘wpcf7′ )
)
after this code:
‘invalid_required’ => array(
‘description’ => __( “There is a field that sender is needed to fill in”, ‘wpcf7′ ),
‘default’ => __( ‘Please fill the required field.’, ‘wpcf7′ )
),
third, i went to the formatting file located at wp-includes/formatting.php and created a new function
function is_telnum($telnum)
{
$regexp = ‘/^[0-9\+\-]{7,}$/’;
if(preg_match($regexp, $telnum))
return true;
else
return false;
}
NOTE:
in my code, i created a function wherein the input text for the telephone number will accept minimum of 7 characters (this only includes numbers and characters + and -)
example of valid inputs:
+1234567
01234567
02-1234567
Now to use it in the contact form 7, please use this tag just
Your Phone



April 26th, 2011
admin
Posted in
Thanks for the nice info.
Thanks! This is so helpful. Whit this tutorial, I managed to add several more field validations
Great work!
really really helpful..i was looking for it
thank you for sharing