我的输入过滤器功能如下:
(function($) {
$.fn.inputFilter = function(inputFilter) {
return this.on("input keydown keyup mousedown mouseup select contextmenu drop",function() {
if (inputFilter(this.value)) {
this.oldValue = this.value;
this.oldSelectionStart = this.selectionStart;
this.oldSelectionEnd = this.selectionEnd;
} else if (this.hasOwnProperty("oldValue")) {
this.value = this.oldValue;
this.setSelectionRange(this.oldSelectionStart,this.oldSelectionEnd);
}
});
};
}(jQuery));
当前我正在使用此正则表达式模式:
^\d*[.,]?\d{0,1}$
返回以下内容:
a == false
1.0 == true
3.5 == true
1.1 == true
2.34 == false
11.0 == true
1.1 == true
1.2 == true
1.3 == true
1.5 == true
1.0 == true
2 == true
预期结果:
a == false
1.0 == true
3.5 == true
1.1 == false
2.34 == false
11.0 == true
1.1 == false
1.2 == false
1.3 == false
1.5 == true
1.0 == true
2 == true
我尝试使用:
^\d*[.,1}[05]$
它在regex测试器站点上似乎是正确的,但是当应用于输入过滤器时,它只允许我键入0和5,而不是其他数字以及点(。)
更新1:添加了JSfiddle链接
任何帮助都很好