Validate Phone Numbers in Fluent Forms
Checks Fluent Forms phone fields for a realistic number of digits and only the characters phone numbers use, instead of accepting any text.
Opens WPIDE on
Code
$wpide_ff_phone_fields = array( 'phone' );
$wpide_ff_phone_min = 7;
$wpide_ff_phone_max = 15;
add_filter( 'fluentform/validation_errors', function ( $errors, $data ) use ( $wpide_ff_phone_fields, $wpide_ff_phone_min, $wpide_ff_phone_max ) {
foreach ( $wpide_ff_phone_fields as $name ) {
$phone = isset( $data[ $name ] ) && is_string( $data[ $name ] ) ? trim( $data[ $name ] ) : '';
if ( '' === $phone ) {
continue;
}
$digits = strlen( preg_replace( '/\D/', '', $phone ) );
if ( ! preg_match( '/^\+?[0-9\s().\-]+$/', $phone ) || $digits < $wpide_ff_phone_min || $digits > $wpide_ff_phone_max ) {
$errors[ $name ] = array( 'Please enter a valid phone number.' );
}
}
return $errors;
}, 10, 2 );
Notes
Set the phone field names at the top; empty optional fields are skipped.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.