Phone number validation Using Contact form 7 wordpess Plugin

i have solved this by means of this codes:

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_get_message( ‘invalid_required’ );
//$result[‘reason’][$name] = $wpcf7_contact_form->message( ‘invalid_required’ );
}
elseif ( ” != $_POST[$name] && ! is_telnum( $_POST[$name] ) ) {
$result[‘valid’] = false;

$result[‘reason’][$name] = wpcf7_get_message( ‘invalid_digit’ );
//$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</td><td>[digit* text-417]

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s