# Custom Pattern Rules for Fluent Forms Fields

> Validates chosen Fluent Forms fields against your own patterns, such as postcodes, order numbers or membership IDs, each with its own error message.

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

## Code

```php
// Field name => array( pattern, error message ).
$wpide_ff_patterns = array(
    'postcode'     => array( '/^[A-Za-z0-9 \-]{3,10}$/', 'Please enter a valid postcode.' ),
    'order_number' => array( '/^#?\d{4,10}$/', 'Order numbers contain 4 to 10 digits.' ),
);

add_filter( 'fluentform/validation_errors', function ( $errors, $data ) use ( $wpide_ff_patterns ) {
    foreach ( $wpide_ff_patterns as $name => $rule ) {
        $value = isset( $data[ $name ] ) && is_string( $data[ $name ] ) ? trim( $data[ $name ] ) : '';

        if ( '' !== $value && ! preg_match( $rule[0], $value ) ) {
            $errors[ $name ] = array( $rule[1] );
        }
    }

    return $errors;
}, 10, 2 );
```

## Notes

Keys are field names (the Name Attribute in the field's Advanced Options); patterns are PHP regular expressions matched against the whole value.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Custom Pattern Rules for Fluent Forms Fields" 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/
