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

Issue with ng-pattern #11917

Closed
ghost opened this issue May 21, 2015 · 4 comments
Closed

Issue with ng-pattern #11917

ghost opened this issue May 21, 2015 · 4 comments

Comments

@ghost
Copy link

ghost commented May 21, 2015

I have an issue with ng-pattern. It works perfectly when adding characters to the input, it vaildates and shows error when invalid but acts weirdly while deleting characters from the input even if its is a valid string. If you delete a character from the input it show invalid and if you delete one more character it shows it as a valid. this repeats for every character you delete.

I observed this issue in 1.2.18, 1.3.15 and 1.4.0.rc2
I have added an example in the plunker.

http://plnkr.co/edit/A4kHHthnEYzaxyVzu7w9?p=preview

@gkalpak
Copy link
Member

gkalpak commented May 21, 2015

This is because ngPattern/pattern tests with regexp.test(modelValue) which remembers that lastIndex of the last match if the g flag is set, so calling it multiple times yields unexpected results (i.e. it is not idempotent).

Using g doesn't make much sense either, so an easy solution is dropping the g flag and everything works as expected. E.g.:

/(\w+\s*)+/    -->  Matches as long as there is at least 1 word-character
                    followed by 0 or more whitespace-characters

/^(\w+\s*)+$/  -->  Matches as long as the input consists exclusively of sequences
                    of at least 1 word-character followed by 0 or more whitespace-characters

'(\w+\s*)+'    -->  Equivalent to the above (because `ngPattern` wraps it
                    in `^...$` automatically, but (negligibly) less performant

@gkalpak
Copy link
Member

gkalpak commented May 21, 2015

/cc @petebacondarwin
I wonder if it makes sense to ignore the g flag in ngPattern in order to avoid such issues (I can't think of a usecase, where using g would make sense).
Alternatively, we could replace regexp.test(modelValue) with modelValue.search(regexp), but then we should ensure that it is a string or convert it to one etc (it might not be worth the trouble).

@Narretz Narretz added this to the Purgatory milestone May 21, 2015
@ghost
Copy link
Author

ghost commented May 21, 2015

@gkalpak Thanks for the suggestions. It helps.

@petebacondarwin
Copy link
Contributor

I think this should just be documented as a weird case that should be avoided. I don't see why you would want to add a g modifier when using ngPattern but I don't think we should be the ones to remove it.

gkalpak added a commit to gkalpak/angular.js that referenced this issue May 22, 2015
Add a note to point out that using the `g` flag on the validation RegExp,
will cause each search to start at the index of the last search's match,
thus not taking the whole input value into account.

Closes angular#11917
gkalpak added a commit to gkalpak/angular.js that referenced this issue May 22, 2015
Add a note to point out that using the `g` flag on the validation RegExp,
will cause each search to start at the index of the last search's match,
thus not taking the whole input value into account.

Closes angular#11917
gkalpak added a commit to gkalpak/angular.js that referenced this issue May 22, 2015
Add a note to point out that using the `g` flag on the validation RegExp,
will cause each search to start at the index of the last search's match,
thus not taking the whole input value into account.

Closes angular#11917
Closes angular#11928
netman92 pushed a commit to netman92/angular.js that referenced this issue Aug 8, 2015
Add a note to point out that using the `g` flag on the validation RegExp,
will cause each search to start at the index of the last search's match,
thus not taking the whole input value into account.

Closes angular#11917
Closes angular#11928
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants