0

Our application is still using AngularJS 1.5.11 but I've tested the included example also with version 1.7.2 and get the same results.

The problem is the strange behavior of the validateNumber function. The validation seems to be flipping between true and false for each character typed. The validateMin and validateMax functions are working as expected.

The example code can be found here: https://codepen.io/kdbruin/pen/MBmXYz

Any insights as to why this is happening?

2 Answers 2

1

Maybe this is the culprit?

test() called multiple times on the same global regular expression instance will advance past the previous match.

MDN

Easiest fix might be to change

var result = numberRegexp.test(modelValue);

to

var result = numberRegexp.search(modelValue) !== -1;
Sign up to request clarification or add additional context in comments.

1 Comment

Found the answer already and it seems that the "g" (global) flag is causing this. But I will try your suggestion as well.
0

It seems that the regular expression is at fault here. When using

var numberRegexp = new RegExp("^[-+]?\\d+(" + (decimalSeparator === "." ? "\\." : decimalSeparator) + "\\d*)?$", "i");

the problem disappears. So it seems that the original "g" flag should not be used.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.