MCPcopy Index your code
hub / github.com/angular/angular / patternValidator

Function patternValidator

packages/forms/src/validators.ts:569–596  ·  view source on GitHub ↗
(pattern: string | RegExp)

Source from the content-addressed store, hash-verified

567 * See `Validators.pattern` for additional information.
568 */
569export function patternValidator(pattern: string | RegExp): ValidatorFn {
570 if (!pattern) return nullValidator;
571 let regex: RegExp;
572 let regexStr: string;
573 if (typeof pattern === 'string') {
574 regexStr = '';
575
576 if (pattern.charAt(0) !== '^') regexStr += '^';
577
578 regexStr += pattern;
579
580 if (pattern.charAt(pattern.length - 1) !== '$') regexStr += '$';
581
582 regex = new RegExp(regexStr);
583 } else {
584 regexStr = pattern.toString();
585 regex = pattern;
586 }
587 return (control: AbstractControl): ValidationErrors | null => {
588 if (isEmptyInputValue(control.value)) {
589 return null; // don't validate empty values to allow optional controls
590 }
591 const value: string = control.value;
592 return regex.test(value)
593 ? null
594 : {'pattern': {'requiredPattern': regexStr, 'actualValue': value}};
595 };
596}
597
598/**
599 * Function that has `ValidatorFn` shape, but performs no operation.

Callers 2

PatternValidatorClass · 0.90
patternMethod · 0.85

Calls 4

isEmptyInputValueFunction · 0.85
charAtMethod · 0.80
toStringMethod · 0.65
testMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…