Last active
November 9, 2017 18:03
-
-
Save alpha1/9fc772fa6d175e3b39b3 to your computer and use it in GitHub Desktop.
Graivty Forms Phone Field Validation - Disallow a phone number consisting of entirely the same digits
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'gform_field_validation', 'wphacks_prevent_fake_phone_numbers', 1, 4); | |
function wphacks_prevent_fake_phone_numbers( $result, $value, $form, $field ){ | |
$error = array( 'is_valid' => 0, 'message' => 'Please use a real phone number' ); | |
//the 3 built in phone number fields, phone and text with input masks | |
if ( ($field->type == 'phone') || ( $field->type == 'text' && $field['inputMaskValue'] == '(999) 999-9999? x99999' ) || ( $field->type == 'text' && $field['inputMaskValue'] == '(999) 999-9999' ) ){ | |
//remove all special characters to make a numbers only string | |
$numbers_only = implode("", array_filter(str_split($value),function($array){ return is_numeric($array); })); | |
//enter bad phone numbers here, without any symbols | |
$bad_phone_numbers = array( | |
'0111111111', | |
'1234567890', | |
'0123456789', | |
'5555551212', | |
); | |
if(in_array($numbers_only, $bad_phone_numbers)){ | |
return $error; | |
} | |
//this keeps | |
if(count( array_unique( array_filter( str_split( $value ),function( $array ){ return is_numeric( $array ); } ) ) ) === 1 ){ | |
return $error; | |
} | |
//this keeps out real area codes with 7 of the same digits | |
if(count( array_unique( array_filter( str_split( substr( $numbers_only, 3) ),function( $array ){ return is_numeric( $array ); } ) ) ) === 1 ){ | |
return array( 'is_valid' => 0, 'message' => 'Please use a real phone number'. substr( $numbers_only, 3, 0 ) ); | |
} | |
} | |
return $result; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment