From 556cf4ed29ff3d90de4b8ee3e92098b2b0c5049c Mon Sep 17 00:00:00 2001 From: Caitlin Potter Date: Mon, 3 Nov 2014 14:37:20 -0500 Subject: [PATCH] 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 #9881 --- src/ng/directive/input.js | 2 +- test/ng/directive/inputSpec.js | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index 639afcb9dfcb..aed77e87b555 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -2577,7 +2577,7 @@ var patternDirective = function() { var regexp, patternExp = attr.ngPattern || attr.pattern; attr.$observe('pattern', function(regex) { if (isString(regex) && regex.length > 0) { - regex = new RegExp(regex); + regex = new RegExp('^' + regex + '$'); } if (regex && !regex.test) { diff --git a/test/ng/directive/inputSpec.js b/test/ng/directive/inputSpec.js index 8d4e47763609..348be5c4f7e1 100644 --- a/test/ng/directive/inputSpec.js +++ b/test/ng/directive/inputSpec.js @@ -2224,6 +2224,21 @@ describe('input', function() { }); }).toThrowMatching(/^\[ngPattern:noregexp\] Expected fooRegexp to be a RegExp but was/); }); + + it('should be invalid if entire string does not match pattern', function() { + compileInput(''); + changeInputValueTo('1234'); + expect(scope.form.test.$error.pattern).not.toBe(true); + expect(inputElm).toBeValid(); + + changeInputValueTo('123'); + expect(scope.form.test.$error.pattern).toBe(true); + expect(inputElm).not.toBeValid(); + + changeInputValueTo('12345'); + expect(scope.form.test.$error.pattern).toBe(true); + expect(inputElm).not.toBeValid(); + }); });