(pattern: string | RegExp)
| 567 | * See `Validators.pattern` for additional information. |
| 568 | */ |
| 569 | export 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. |
no test coverage detected
searching dependent graphs…