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

Commit c458988

Browse files
committed
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 04cc1d2 commit c458988

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/ng/directive/input.js

Lines changed: 5 additions & 3 deletions
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

Lines changed: 14 additions & 2 deletions
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$/;
@@ -496,9 +508,9 @@ describe('input', function() {
496508
changeInputValueTo('x');
497509
expect(inputElm).toBeInvalid();
498510

499-
scope.regexp = /abc?/;
511+
scope.regexp = /abc?/i;
500512

501-
changeInputValueTo('ab');
513+
changeInputValueTo('aB');
502514
expect(inputElm).toBeValid();
503515

504516
changeInputValueTo('xx');

0 commit comments

Comments
 (0)