Contact Form 7 Phone Validation
Using the below code in your themes functions.php would add a new phone validation to your Contact Form 7 Plugin. The following code can also be used as a reference to add any other validation to the Contact Form 7 Plugin.
<?php
/* Contact Form 7 Phone Validation */if(function_exists(‘wpcf7_add_shortcode’)){
wpcf7_add_shortcode( ‘phone’, ‘wpcf7_phone_shortcode_handler’, true );
wpcf7_add_shortcode( ‘phone*’, ‘wpcf7_phone_shortcode_handler’, true );
}function wpcf7_phone_shortcode_handler( $tag ) {
if ( ! is_array( $tag ) )
return ”;$type = $tag['type'];
$name = $tag['name'];
$options = (array) $tag['options'];
$values = (array) $tag['values'];if ( empty( $name ) )
return ”;$atts = ”;
$id_att = ”;
$class_att = ”;
$size_att = ”;
$maxlength_att = ”;
$tabindex_att = ”;
$title_att = ”;$class_att .= ‘ wpcf7-text’;
if ( ‘phone’ == $type || ‘phone*’ == $type )
$class_att .= ‘ wpcf7-validates-as-phone’;if ( ‘phone*’ == $type )
$class_att .= ‘ wpcf7-validates-as-required’;foreach ( $options as $option ) {
if ( preg_match( ‘%^id:([-0-9a-zA-Z_]+)$%’, $option, $matches ) ) {
$id_att = $matches[1];} elseif ( preg_match( ‘%^class:([-0-9a-zA-Z_]+)$%’, $option, $matches ) ) {
$class_att .= ‘ ‘ . $matches[1];} elseif ( preg_match( ‘%^([0-9]*)[/x]([0-9]*)$%’, $option, $matches ) ) {
$size_att = (int) $matches[1];
$maxlength_att = (int) $matches[2];} elseif ( preg_match( ‘%^tabindex:(\d+)$%’, $option, $matches ) ) {
$tabindex_att = (int) $matches[1];}
}$value = (string) reset( $values );
if ( wpcf7_script_is() && preg_grep( ‘%^watermark$%’, $options ) ) {
$class_att .= ‘ wpcf7-use-title-as-watermark’;
$title_att .= sprintf( ‘ %s’, $value );
$value = ”;
}if ( wpcf7_is_posted() && isset( $_POST[$name] ) )
$value = stripslashes_deep( $_POST[$name] );if ( $id_att )
$atts .= ‘ id=”‘ . trim( $id_att ) . ‘”‘;if ( $class_att )
$atts .= ‘ class=”‘ . trim( $class_att ) . ‘”‘;if ( $size_att )
$atts .= ‘ size=”‘ . $size_att . ‘”‘;
else
$atts .= ‘ size=”40″‘; // default sizeif ( $maxlength_att )
$atts .= ‘ maxlength=”‘ . $maxlength_att . ‘”‘;if ( ” !== $tabindex_att )
$atts .= sprintf( ‘ tabindex=”%d”‘, $tabindex_att );if ( $title_att )
$atts .= sprintf( ‘ title=”%s”‘, trim( esc_attr( $title_att ) ) );$html = ‘<input type=”text” name=”‘ . $name . ‘” value=”‘ . esc_attr( $value ) . ‘”‘ . $atts . ‘ />’;
$validation_error = wpcf7_get_validation_error( $name );
$html = ‘<span class=”wpcf7-form-control-wrap ‘ . $name . ‘”>’ . $html . $validation_error . ‘</span>’;
return $html;
}add_filter( ‘wpcf7_validate_phone’, ‘wpcf7_phone_validation_filter’, 10, 2 );
add_filter( ‘wpcf7_validate_phone*’, ‘wpcf7_phone_validation_filter’, 10, 2 );function wpcf7_phone_validation_filter( $result, $tag ) {
$type = $tag['type'];
$name = $tag['name'];$_POST[$name] = trim( strtr( (string) $_POST[$name], “\n”, ” ” ) );
if ( ‘phone’ == $type || ‘phone*’ == $type ) {
if ( ‘phone*’ == $type && ” == $_POST[$name] ) {
$result['valid'] = false;
$result['reason'][$name] = wpcf7_get_message( ‘invalid_required’ );
} elseif ( ” != $_POST[$name] && ! is_telnum( $_POST[$name] ) ) {
$result['valid'] = false;
$result['reason'][$name] = wpcf7_get_message( ‘invalid_phone’ );
}
}return $result;
}function is_telnum($telnum)
{
$regexp = ‘/^[0-9\+\-\)\(\d\s]{7,}$/’; // Change the regex here suiting your phone number formatif(preg_match($regexp, $telnum))
return true;
else
return false;
}/* Tag generator */
function wpcf7_add_tag_generator_phone() {
if( function_exists(‘wpcf7_add_tag_generator’) ){
wpcf7_add_tag_generator( ‘phone’, __( ‘Phone field’, ‘wpcf7′ ),
‘wpcf7-tg-pane-phone’, ‘wpcf7_tg_pane_phone_’ );
}
}function wpcf7_tg_pane_phone_( &$contact_form ) {
wpcf7_tg_pane_phone( ‘phone’ );
}function wpcf7_tg_pane_phone( $type = ‘phone’ ) { ?>
<div id=”wpcf7-tg-pane-<?php echo $type; ?>” class=”hidden”>
<form action=”">
<table>
<tr><td><input type=”checkbox” name=”required” /> <?php echo esc_html( __( ‘Required field?’, ‘wpcf7′ ) ); ?></td></tr>
<tr><td><?php echo esc_html( __( ‘Name’, ‘wpcf7′ ) ); ?><br /><input type=”text” name=”name” class=”tg-name oneline” /></td><td></td></tr>
</table><table>
<tr>
<td><code>id</code> (<?php echo esc_html( __( ‘optional’, ‘wpcf7′ ) ); ?>)<br />
<input type=”text” name=”id” class=”idvalue oneline option” /></td><td><code>class</code> (<?php echo esc_html( __( ‘optional’, ‘wpcf7′ ) ); ?>)<br />
<input type=”text” name=”class” class=”classvalue oneline option” /></td>
</tr><tr>
<td><code>size</code> (<?php echo esc_html( __( ‘optional’, ‘wpcf7′ ) ); ?>)<br />
<input type=”text” name=”size” class=”numeric oneline option” /></td><td><code>maxlength</code> (<?php echo esc_html( __( ‘optional’, ‘wpcf7′ ) ); ?>)<br />
<input type=”text” name=”maxlength” class=”numeric oneline option” /></td>
</tr><tr>
<td><?php echo esc_html( __( ‘Default value’, ‘wpcf7′ ) ); ?> (<?php echo esc_html( __( ‘optional’, ‘wpcf7′ ) ); ?>)<br /><input type=”text” name=”values” class=”oneline” /></td><td>
<br /><input type=”checkbox” name=”watermark” class=”option” /> <?php echo esc_html( __( ‘Use this text as watermark?’, ‘wpcf7′ ) ); ?>
</td>
</tr>
</table><div class=”tg-tag”><?php echo esc_html( __( “Copy this code and paste it into the form left.”, ‘wpcf7′ ) ); ?><br /><input type=”text” name=”<?php echo $type; ?>” class=”tag” readonly=”readonly” onfocus=”this.select()” /></div>
<div class=”tg-mail-tag”><?php echo esc_html( __( “And, put this code into the Mail fields below.”, ‘wpcf7′ ) ); ?><br /><span class=”arrow”>⬇</span> <input type=”text” class=”mail-tag” readonly=”readonly” onfocus=”this.select()” /></div>
</form>
</div><?php
}add_action( ‘admin_init’, ‘wpcf7_add_tag_generator_phone’, 15 );
add_filter( ‘wpcf7_messages’, ‘wpcf_add_phone_validation_message’ );
function wpcf_add_phone_validation_message( $message ) {
$message['invalid_phone'] = array(
‘description’ => ‘Phone number format is invalid’,
‘default’ => ‘The phone number format is invalid.’
) ;
return $message;
}
?>
curdaneta 11:57 pm on March 14, 2012 Permalink | Log in to Reply
Thanks for the code Joshua, but looks like it’s messed up after
could you please post it again
Regards
Ciro
curdaneta 11:57 pm on March 14, 2012 Permalink | Log in to Reply
sorry I meant
curdaneta 11:58 pm on March 14, 2012 Permalink | Log in to Reply
…. div id = wpcf7-tg-pane- class= hidden ….