Graivty Forms Phone Field Validation - Disallow a phone number consisting of entirely the same digits
<?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;
}
?>