{"record":{"id":"b8808b6f39bdf9c8","repo":"dotnet/aspnetcore","slug":"regex-validator-requires-a-non-empty-pattern-par","errorCode":null,"errorMessage":"regex validator requires a non-empty \"pattern\" parameter.","messagePattern":"regex validator requires a non-empty \"pattern\" parameter\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/Components/Web.JS/src/Validation/Validators/Regex.ts","lineNumber":12,"sourceCode":"// Licensed to the .NET Foundation under one or more agreements.\n// The .NET Foundation licenses this file to you under the MIT license.\n\nimport { ValidationContext, ValidationResult, Validator, pass, fail } from '../ValidationTypes';\n\n// Validates that the value matches a regular expression pattern (full match).\n// The pattern is anchored with ^(?:...)$ to match .NET's exact-match semantics.\n// Throws if the mandatory `pattern` parameter is missing.\nexport const regexValidator: Validator = (context: ValidationContext): ValidationResult => {\n  const { value, params } = context;\n  if (!params.pattern) {\n    throw new Error('regex validator requires a non-empty \"pattern\" parameter.');\n  }\n\n  if (!value) {\n    return pass();\n  }\n\n  // Anchor the pattern for full-match semantics, matching .NET's RegularExpressionAttribute\n  // which requires Index == 0 && Length == value.Length. The non-capturing group avoids\n  // changing semantics for patterns with alternation (e.g. \"a|b\").\n  const anchored = `^(?:${params.pattern})$`;\n\n  try {\n    return new RegExp(anchored).test(value) ? pass() : fail();\n  } catch {\n    return pass();\n  }\n};\n","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/3600ca084e9c8b5f4174fc5e747f4c52d2100806/src/Components/Web.JS/src/Validation/Validators/Regex.ts#L1-L30","documentation":"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.","triggerScenarios":"Thrown at line 12 when params.pattern is falsy — the validator was registered (data-val-regex-pattern) without a pattern value.","commonSituations":"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.","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."],"exampleFix":"<!-- before -->\n<input name=\"Code\" data-val=\"true\" data-val-regex=\"Invalid code\" />\n<!-- after -->\n<input name=\"Code\" data-val=\"true\" data-val-regex=\"Invalid code\" data-val-regex-pattern=\"^[A-Z]{4}$\" />","handlingStrategy":"validation","validationCode":"function regexConfigured(params: any): boolean {\n  return !!params && typeof params.pattern === 'string' && params.pattern.length > 0;\n}","typeGuard":"function hasPatternParam(p: unknown): p is { pattern: string } {\n  return !!p && typeof (p as any).pattern === 'string' && (p as any).pattern.length > 0;\n}","tryCatchPattern":null,"preventionTips":["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."],"tags":["blazor","validation","regex","form-validation","misconfiguration"],"backgroundTag":null,"analyzedSha":"3600ca084e9c8b5f4174fc5e747f4c52d2100806","analyzedAt":"2026-08-11T16:32:30.678Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}