mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-17 07:40:22 +00:00
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
This commit is contained in:
parent
a91405889f
commit
12b6deb1ce
2 changed files with 17 additions and 3 deletions
|
|
@ -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)) {
|
||||
|
|
@ -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)
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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$/;
|
||||
|
|
|
|||
Loading…
Reference in a new issue