Skip to content

Commit 75f8522

Browse files
ghetolayalexeagle
authored andcommitted
feat(forms): handle string with and without line boundary on pattern validator (#19256)
PR Close #19256
1 parent a771ee5 commit 75f8522

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

packages/forms/src/validators.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,14 @@ export class Validators {
163163
let regex: RegExp;
164164
let regexStr: string;
165165
if (typeof pattern === 'string') {
166-
regexStr = `^${pattern}$`;
166+
regexStr = '';
167+
168+
if (pattern.charAt(0) !== '^') regexStr += '^';
169+
170+
regexStr += pattern;
171+
172+
if (pattern.charAt(pattern.length - 1) !== '$') regexStr += '$';
173+
167174
regex = new RegExp(regexStr);
168175
} else {
169176
regexStr = pattern.toString();

packages/forms/test/validators_spec.ts

+6
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,12 @@ import {map} from 'rxjs/operator/map';
270270

271271
it('should not error on "undefined" pattern',
272272
() => expect(Validators.pattern(undefined !)(new FormControl('aaAA'))).toBeNull());
273+
274+
it('should work with string containing line boundary',
275+
() => expect(Validators.pattern('^[aA]*$')(new FormControl('aaAA'))).toBeNull());
276+
277+
it('should work with string not containing line boundary',
278+
() => expect(Validators.pattern('[aA]*')(new FormControl('aaAA'))).toBeNull());
273279
});
274280

275281
describe('compose', () => {

0 commit comments

Comments
 (0)