dotnet/aspnetcore · error

regex validator requires a non-empty "pattern" parameter.

Error message

regex validator requires a non-empty "pattern" parameter.

What it means

Thrown by the regex validator (Regex.ts:12) when params.pattern is falsy. This validator enforces [RegularExpression] semantics — it anchors the pattern with ^(?:...)$ for a full match against the field value. Without a pattern there is nothing to match, so it throws rather than treating empty as match-anything.

Source

Thrown at src/Components/Web.JS/src/Validation/Validators/Regex.ts:12

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

import { ValidationContext, ValidationResult, Validator, pass, fail } from '../ValidationTypes';

// Validates that the value matches a regular expression pattern (full match).
// The pattern is anchored with ^(?:...)$ to match .NET's exact-match semantics.
// Throws if the mandatory `pattern` parameter is missing.
export const regexValidator: Validator = (context: ValidationContext): ValidationResult => {
  const { value, params } = context;
  if (!params.pattern) {
    throw new Error('regex validator requires a non-empty "pattern" parameter.');
  }

  if (!value) {
    return pass();
  }

  // Anchor the pattern for full-match semantics, matching .NET's RegularExpressionAttribute
  // which requires Index == 0 && Length == value.Length. The non-capturing group avoids
  // changing semantics for patterns with alternation (e.g. "a|b").
  const anchored = `^(?:${params.pattern})$`;

  try {
    return new RegExp(anchored).test(value) ? pass() : fail();
  } catch {
    return pass();
  }
};

View on GitHub (pinned to 294cab2f9b)

Solutions

  1. Add [RegularExpression(@"^[A-Za-z0-9]+$")] and confirm the input renders data-val-regex-pattern="^[A-Za-z0-9]+$" (and data-val-regex for the error message).
  2. When wiring manually, pass params: { pattern: '^[A-Za-z0-9]+$' } — the validator adds the ^(?:...)$ anchoring itself, so supply the raw inner pattern.
  3. Escape backslashes correctly in C# verbatim strings vs JS, and verify the rendered attribute is not HTML-encoded in a way that breaks the regex.
  4. Ensure no tag helper strips data-val-regex-pattern.

Example fix

// before
<input name="Code" data-val-regex />

// after
<input name="Code" data-val-regex data-val-regex-pattern="^[A-Z]{4}$" />
Defensive patterns

Strategy: validation

Validate before calling

function hasPatternParam(params: Record<string, unknown>): boolean {
  return Boolean(params && typeof params.pattern === 'string' && params.pattern.length > 0);
}

Type guard

function isRegexConfigured(p: any): p is { pattern: string } {
  return p && typeof p.pattern === 'string' && p.pattern.length > 0;
}

Prevention

When it happens

Trigger: Invoking regexValidator with a params object missing 'pattern' or pattern='' / null. Emitted from [RegularExpression("pattern")]; if the data-val-regex-pattern metadata is absent the JS side has no pattern.

Common situations: Model has [RegularExpression] but the pattern argument is empty or stripped. Custom form rendering drops data-val-regex-pattern while keeping data-val-regex. Manually registering the validator without a pattern. Razor that conditionally renders the attribute on the wrong branch.

Related errors


AI-assisted analysis of dotnet/aspnetcore@294cab2f9b (2026-08-06). Data as JSON: /api/errors/b8808b6f39bdf9c8. Report an issue: GitHub.