Skip to content

Instantly share code, notes, and snippets.

@barbwiredmedia
Last active June 18, 2018 04:30
Show Gist options
  • Save barbwiredmedia/6312348 to your computer and use it in GitHub Desktop.
Save barbwiredmedia/6312348 to your computer and use it in GitHub Desktop.
Wordpress - Functions: CF7 cf7 contact form 7- Validate Phone Numbers and custom matching password
/*add in functions.php
Validate Numbers in Contact Form 7
This is for 10 digit numbers - [phone] for feild
*/
function is_number( $result, $tag ) {
$type = $tag['type'];
$name = $tag['name'];
if ($name == 'phone' || $name == 'fax') { // Validation applies to these textfield names. Add more with || inbetween
$stripped = preg_replace( '/\D/', '', $_POST[$name] );
$_POST[$name] = $stripped;
if( strlen( $_POST[$name] ) != 10 ) { // Number string must equal this
$result['valid'] = false;
$result['reason'][$name] = $_POST[$name] = 'Please enter a 10 digit phone number.';
}
}
return $result;
}
add_filter( 'wpcf7_validate_text', 'is_number', 10, 2 );
add_filter( 'wpcf7_validate_text*', 'is_number', 10, 2 );
/*
Create Custom matching text area
<div class="passcode">[text* presscode /15 placeholder "PRESS CODE"]</div>
*/
function is_matching($result, $tag) {
$type = $tag['type'];
$name = $tag['name'];
$password = '0123456789';
if ($name == 'presscode') { // Validation applies to these textfield names. Add more with || inbetween
$stripped = preg_replace('/\D/', '', $_POST[$name]);
$_POST[$name] = $stripped;
if (strlen($_POST[$name]) != 10) { // Number string must equal this
$result['valid'] = false;
$result['reason'][$name] = $_POST[$name] = 'Please enter a valid pass code';
}
if ($_POST[$name] !=$password) { // Number string must equal this
$result['valid'] = false;
$result['reason'][$name] = $_POST[$name] = 'Please enter a valid pass code';
}
}
return $result;
}
add_filter('wpcf7_validate_text', 'is_matching', 10, 2);
add_filter('wpcf7_validate_text*', 'is_matching', 10, 2);
@arshidkv12
Copy link

Addon for contact form 7 phone number validation (Telephone shortcode)
https://ciphercoin.com/downloads/advanced-contact-form-7-telephone-input/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment