Angular regex test not consistent ng-pattern validation on input

I have a ng-pattern validation on my input


This does not work the same everytime. And shows very weird behaviour.

See plnkr

The ngMessage is only shown on every second character I type. But the console evaluates the regex test correctly.

I set a breakpoint in the browser dev tool on the line in angular that tests the regex and did some debugging in the console. Here is the console output from my debugging. The debugger did not move while I was using the console so no values had been changed. (-> represents my input, <- is what the debugger spits out)

-> regexp.test(viewValue)
<- false

-> regexp
<- /^(.+)$/g

-> /^(.+)$/g.test(viewValue)
<- true

-> regexp.test(viewValue)
<- true

Can someone tell me what is going on? Note how regexp.test(viewvalue) was false first, then was true on the last test without anything changing.



You must remove the /g from the regex, see updated plunkr.

According to the docs:

Note: Avoid using the g flag on the RegExp, as it will cause each successive search to start at the index of the last search's match, thus not taking the whole input value into account.


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.