Our web form built with Gravity Forms on WordPress uses a "single line text" field type for the phone number field.
How can I add a custom validation to this so that it will only allow submission of integers and only if there is 10 or more input?
This is because in Australia our phone numbers (without country code) are 10 digits. We're currently getting responses containing letters or incomplete numbers.
I've just implemented the below function but it doesn't work.
// Form Phone Field Validation to use numbers only with minimum 10 digits
add_filter( 'gform_field_validation_16_4', 'custom_validation', 10, 4 ); //Replace the ## with your form ID
function custom_validation( $result, $value, $form, $field ) {
$value = str_replace(array('(',')','-',' '),'',$value);//Remove hyphens, parenthesis, and spaces - you can take this out if you want strict numbers only
if ( $result['is_valid'] && strlen( $value ) < 10 && is_numeric( $value ) ) {
$result['is_valid'] = false;
$result['message'] = 'Please enter a valid phone number of 10 or more digits with area code';
}
return $result;
}