Strange validator behaviour in AngularJS 1.5.11

时间:2018-07-24 10:04:14

标签: angularjs angularjs-directive angularjs-validation

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 个答案:

答案 0 :(得分:1)

也许是罪魁祸首?

  在同一全局正则表达式实例上多次调用的

test()将前进到之前的匹配项。

MDN

最简单的解决方法可能就是更改

var result = numberRegexp.test(modelValue);

var result = numberRegexp.search(modelValue) !== -1;

答案 1 :(得分:0)

这里的正则表达式似乎有问题。使用时

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

问题消失了。因此,似乎不应使用原始的“ g”标志。