Skip to content

Commit 556cf4e

Browse files
committed
fix(ngPattern): match behaviour of native HTML pattern attribute
From https://html.spec.whatwg.org/multipage/forms.html#attr-input-pattern > The compiled pattern regular expression, when matched against a string, must have its start anchored to the start of the string and its end anchored to the end of the string. Closes angular#9881
1 parent d906ed3 commit 556cf4e

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/ng/directive/input.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2577,7 +2577,7 @@ var patternDirective = function() {
25772577
var regexp, patternExp = attr.ngPattern || attr.pattern;
25782578
attr.$observe('pattern', function(regex) {
25792579
if (isString(regex) && regex.length > 0) {
2580-
regex = new RegExp(regex);
2580+
regex = new RegExp('^' + regex + '$');
25812581
}
25822582

25832583
if (regex && !regex.test) {

test/ng/directive/inputSpec.js

+15
Original file line numberDiff line numberDiff line change
@@ -2224,6 +2224,21 @@ describe('input', function() {
22242224
});
22252225
}).toThrowMatching(/^\[ngPattern:noregexp\] Expected fooRegexp to be a RegExp but was/);
22262226
});
2227+
2228+
it('should be invalid if entire string does not match pattern', function() {
2229+
compileInput('<input type="text" name="test" ng-model="value" pattern="\\d{4}">');
2230+
changeInputValueTo('1234');
2231+
expect(scope.form.test.$error.pattern).not.toBe(true);
2232+
expect(inputElm).toBeValid();
2233+
2234+
changeInputValueTo('123');
2235+
expect(scope.form.test.$error.pattern).toBe(true);
2236+
expect(inputElm).not.toBeValid();
2237+
2238+
changeInputValueTo('12345');
2239+
expect(scope.form.test.$error.pattern).toBe(true);
2240+
expect(inputElm).not.toBeValid();
2241+
});
22272242
});
22282243

22292244

0 commit comments

Comments
 (0)