dotnet/aspnetcore · error · Error
regex validator requires a non-empty "pattern" parameter.
Error message
regex validator requires a non-empty "pattern" parameter.
What it means
The Regex validator (client-side counterpart of [RegularExpression]) requires a 'pattern' parameter — the regex the value must fully match (the framework anchors it with ^(?:...)$ to mirror .NET's exact-match semantics). Without a pattern the validator has nothing to test and throws before evaluating the value. The check runs first so the misconfiguration is loud.
Solutions
- Ensure the input renders data-val-regex-pattern="[A-Za-z]+" (your pattern).
- Confirm the C# property has [RegularExpression(@"[A-Za-z]+")] so the tag helper emits the attribute.
- If registering manually, pass { pattern: '[A-Za-z]+' } in params.
- Verify no HTML sanitizer is stripping data-val-regex-* attributes.
Example fix
<!-- before -->
<input name="Code" data-val="true" data-val-regex="Invalid code" />
<!-- after -->
<input name="Code" data-val="true" data-val-regex="Invalid code" data-val-regex-pattern="^[A-Z]{4}$" /> Defensive patterns
Strategy: validation
Validate before calling
function regexConfigured(params: any): boolean {
return !!params && typeof params.pattern === 'string' && params.pattern.length > 0;
} Type guard
function hasPatternParam(p: unknown): p is { pattern: string } {
return !!p && typeof (p as any).pattern === 'string' && (p as any).pattern.length > 0;
} Prevention
- Use [RegularExpression(@"pattern")] on the model so tag helpers emit data-val-regex-pattern.
- Verify rendered inputs include data-val-regex-pattern.
- Ensure HTML sanitizers preserve data-val-regex-* attributes.
- Pass a pattern when registering the validator manually.
When it happens
Trigger: Thrown at line 12 when params.pattern is falsy — the validator was registered (data-val-regex-pattern) without a pattern value.
Common situations: A [RegularExpression(...)] attribute whose data-val-regex-pattern attribute was stripped or never emitted; a hand-built input with data-val-regex but no pattern; tag helper/sanitizer removing data-* attributes.
Related errors
- EqualTo validator requires a non-empty "other" parameter.
- FileExtensions validator requires a non-empty "extensions"…
- length/minlength/maxlength validator requires at least one…
- Range validator requires at least one of "min" or "max"…
- assembly must be defined when using a descriptor.
AI-assisted analysis of dotnet/aspnetcore@3600ca084e (2026-08-11).
Data as JSON: /api/errors/b8808b6f39bdf9c8.
Report an issue: GitHub.
Appendix: 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 3600ca084e)