Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 12b6deb

Browse files
austingrecovojtajina
authored andcommitted
fix(ngPattern): allow modifiers on inline ng-pattern
Add support for regex modifiers on inline `ng-pattern`. `ng-pattern="/regex/i"` now validates correctly. Closes #1437
1 parent a914058 commit 12b6deb

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/ng/directive/input.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,8 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
439439

440440
// pattern validator
441441
var pattern = attr.ngPattern,
442-
patternValidator;
442+
patternValidator,
443+
match;
443444

444445
var validate = function(regexp, value) {
445446
if (isEmpty(value) || regexp.test(value)) {
@@ -452,8 +453,9 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
452453
};
453454

454455
if (pattern) {
455-
if (pattern.match(/^\/(.*)\/$/)) {
456-
pattern = new RegExp(pattern.substr(1, pattern.length - 2));
456+
match = pattern.match(/^\/(.*)\/([gim]*)$/);
457+
if (match) {
458+
pattern = new RegExp(match[1], match[2]);
457459
patternValidator = function(value) {
458460
return validate(pattern, value)
459461
};

test/ng/directive/inputSpec.js

+12
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,18 @@ describe('input', function() {
476476
});
477477

478478

479+
it('should validate in-lined pattern with modifiers', function() {
480+
compileInput('<input type="text" ng-model="value" ng-pattern="/^abc?$/i" />');
481+
scope.$digest();
482+
483+
changeInputValueTo('aB');
484+
expect(inputElm).toBeValid();
485+
486+
changeInputValueTo('xx');
487+
expect(inputElm).toBeInvalid();
488+
});
489+
490+
479491
it('should validate pattern from scope', function() {
480492
compileInput('<input type="text" ng-model="value" ng-pattern="regexp" />');
481493
scope.regexp = /^\d\d\d-\d\d-\d\d\d\d$/;

0 commit comments

Comments
 (0)