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

fix(ngPattern): allow modifiers on inline ng-pattern #2294

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,8 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {

// pattern validator
var pattern = attr.ngPattern,
patternValidator;
patternValidator,
match;

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

if (pattern) {
if (pattern.match(/^\/(.*)\/$/)) {
pattern = new RegExp(pattern.substr(1, pattern.length - 2));
match = pattern.match(/^\/(.*)\/([gim]*)$/);
if (match) {
pattern = new RegExp(match[1], match[2]);
patternValidator = function(value) {
return validate(pattern, value)
};
Expand Down
16 changes: 14 additions & 2 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,18 @@ describe('input', function() {
});


it('should validate in-lined pattern with modifiers', function() {
compileInput('<input type="text" ng-model="value" ng-pattern="/^abc?$/i" />');
scope.$digest();

changeInputValueTo('aB');
expect(inputElm).toBeValid();

changeInputValueTo('xx');
expect(inputElm).toBeInvalid();
});


it('should validate pattern from scope', function() {
compileInput('<input type="text" ng-model="value" ng-pattern="regexp" />');
scope.regexp = /^\d\d\d-\d\d-\d\d\d\d$/;
Expand All @@ -496,9 +508,9 @@ describe('input', function() {
changeInputValueTo('x');
expect(inputElm).toBeInvalid();

scope.regexp = /abc?/;
scope.regexp = /abc?/i;

changeInputValueTo('ab');
changeInputValueTo('aB');
expect(inputElm).toBeValid();

changeInputValueTo('xx');
Expand Down