我想写一个允许只有数字并自动转换为逗号分隔形式的jQuery脚本。我写了这个代码(这是部分代码):限制输入以逗号分隔的形式输入数字
function FillNumericalField(e, inp, CommaSeperated) {
var MainCharKey = e.which.toString();
var IsShiftPresses = e.shiftKey;
if (dict_NumbersOnly[MainCharKey] == null || dict_NumbersOnly[MainCharKey] == 'undefined') {
e.preventDefault();
}
else {
var NewChar = '';
if (isValidDigitToShow(dict_NumbersOnly, MainCharKey)) {
e.preventDefault();
NewChar = dict_NumbersOnly[MainCharKey];
setValue(inp, NewChar, CommaSeperated);
}
}
}
$(document).on('keydown', '.NumbersOnly', function (e) {
FillNumericalField(e, $(this), false);
});
和逗号分拆:
function CommaSeperation(str) {
return str.replace(/,/g, '').replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
}
的问题是,当我要在众多的年底进入数字(例如'123')光标在3之后保持不为4.
以前是否有人写过类似的代码?
感谢
你可以快速的jsfiddle好吗?将有助于解决您的问题很多 – prog1011 2014-11-06 12:47:51