Problem/Motivation

Using a regex with a space in it causes problems.

Steps to reproduce

Create a Regex validation that looks for the word "and": /(.*) and (.*)/
Submit a string that contains the letters "and", e.g. the name "Alexander".

What should happen:
The string is ignored and does not trigger a validation failure.

What actually happens:
The string fails validation.

Proposed resolution

Fix the bug.

Remaining tasks

Work out the cause of the bug.

User interface changes

Regex validation works as expected.

API changes

TBD

Data model changes

TBD

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

DamienMcKenna created an issue. See original summary.

damienmckenna’s picture

Title: Regex doesn't work as expected » Regex rule doesn't work as expected
damienmckenna’s picture

FWIW I ended up building the logic I needed via a custom validator.

abhisekmazumdar made their first commit to this issue’s fork.

abhisekmazumdar’s picture

Status: Active » Needs review

The Regex rule only supported "value must match the pattern" (allow-list) checking. There was no way to invert it to "value must NOT match" (block-list), which is what the original report needed.

MR!26 adds a checkbox to invert the match. Checked by default, so existing rules are unaffected.

How to test

  1. Create a Regex rule with pattern /(.*) and (.*)/, checkbox unchecked.
  2. "Alexander" now passes.
  3. "Bill and Ted" now fails.

Also added automated tests covering this. Marking Needs Review.

arianraeesi’s picture

arianraeesi’s picture

Issue tags: -2027Sprint17 +2026Sprint17
arianraeesi’s picture

Issue tags: +2026Sprint18
kieran.cott’s picture

Manual testing

Configure rule

1. Created field validation rule on Article bundle (Title, Pattern /(.*) and (.*)/):

'Value must match the pattern to be valid': Unchecked

Verify persistence

1. Click 'Edit' on rule
2. 'Value must match the pattern to be valid' - still unchecked

Verify stored value

1. drush cget field_validation.rule_set.node_article field_validation_rules<code>:

data: match: false

Test the regression

Alexander - article saves successfully
Band Aid - article saves successfully
Bill and Ted - submission rejected
Salt and pepper - submission rejected

Test normal matching

1. Edit the validation rule on Article bundle (Title, Pattern /^[A-Za-z]+$/):

'Value must match the pattern to be valid': Checked

Alphabetic - article saves successfully
1234 - submission rejected
Letters123 - submission rejected

Test direct validation

1. Edit the validation rule on Article bundle (Title, Pattern /(.*) and (.*)/):

'Value must match the pattern to be valid': Unchecked
'Validate mode': Direct

Alexander - article saves successfully
Band Aid - article saves successfully
Bill and Ted - submission rejected
Salt and pepper - submission rejected

Testing passed.

abhisekmazumdar’s picture

Status: Needs review » Reviewed & tested by the community

MR !26 is merged.

The Regex rule only supported "value must match the pattern" checking, so there was no way to flag values that contain a pattern like "and" without also failing values like "Alexander" that merely contain the letters. This adds an invert-match option so the rule can go either way.

Thanks all.

abhisekmazumdar’s picture

Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.