# Stricter Phone Validation in Gravity Forms

> Checks Gravity Forms phone fields set to the international format for a realistic number of digits and only valid characters, instead of accepting any text.

- Type: PHP
- Runs: Run everywhere
- Category: [Forms](https://wpide.com/library/category/forms/)
- Tags: gravity-forms, validation, phone
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/gf-phone-validation/

## Code

```php
$wpide_gf_phone_min = 7;
$wpide_gf_phone_max = 15;

add_filter( 'gform_field_validation', function ( $result, $value, $form, $field ) use ( $wpide_gf_phone_min, $wpide_gf_phone_max ) {
    if ( 'phone' !== $field->type || ! $result['is_valid'] || ( ! empty( $field->phoneFormat ) && 'standard' === $field->phoneFormat ) ) {
        return $result;
    }

    $phone = trim( (string) $value );
    if ( '' === $phone ) {
        return $result;
    }

    $digits = strlen( preg_replace( '/\D/', '', $phone ) );

    if ( ! preg_match( '/^\+?[0-9\s().\-]+$/', $phone ) || $digits < $wpide_gf_phone_min || $digits > $wpide_gf_phone_max ) {
        $result['is_valid'] = false;
        $result['message']  = 'Please enter a valid phone number, including the country code.';
    }

    return $result;
}, 10, 4 );
```

## Notes

Fields using the US (###) ###-#### format keep Gravity Forms' own check; adjust the digit range for your country.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Stricter Phone Validation in Gravity Forms" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
