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

Commit

Permalink
fix(ngPattern): allow modifiers on inline ng-pattern
Browse files Browse the repository at this point in the history
Add support for regex modifiers on inline `ng-pattern`.
`ng-pattern="/regex/i"` now validates correctly.

Closes #1437
  • Loading branch information
austingreco authored and vojtajina committed Apr 16, 2013
1 parent a914058 commit 12b6deb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
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
12 changes: 12 additions & 0 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 Down

0 comments on commit 12b6deb

Please sign in to comment.