如何通过jquery检查用户在文本框中输入的小数分隔符
我有插件格式号。我想改进它。如果用户在文本框中输入了一位小数分隔符,则用户不要在文本框中输入“,”或“.”。
例如:不允许用户输入 11111,55,,.44.. -->允许用户输入:11111,5554455
这是我的插件:
$.fn.myPlugin = function(options)
{
options = $.extend({}, {
thousands: '.',
decimal: ','
}, options);
this.keyup(function()
{
$(this).val(function(el, val)
{
val = val.replace(/[^\d.,]/g, '').split(options.decimal);
val[0] = val[0].replace(options.decimal === '.' ? /,/g : /\./g, '');
val[0] = val[0].replace(/(\d)(?=(\d{3})+$)/g, "$1" + options.thousands);
return val.join(options.decimal);
});
});
};
I have the plugin format number. And i want to improve it. If user inputted one decimal separator in the textbox , user do not input "," or "." in the textbox.
ex: no allow user input 11111,55,,.44.. --> allow user input:11111,5554455
Here my plugin:
$.fn.myPlugin = function(options)
{
options = $.extend({}, {
thousands: '.',
decimal: ','
}, options);
this.keyup(function()
{
$(this).val(function(el, val)
{
val = val.replace(/[^\d.,]/g, '').split(options.decimal);
val[0] = val[0].replace(options.decimal === '.' ? /,/g : /\./g, '');
val[0] = val[0].replace(/(\d)(?=(\d{3})+$)/g, "$1" + options.thousands);
return val.join(options.decimal);
});
});
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想这就是你要找的。每当您输入多个句点或逗号时,它就会将其删除。
http://jsfiddle.net/elclanrs/t8G2q/4/
I suppose this is what you're looking for. Whenever you input more than one period or comma it'll just remove it.
http://jsfiddle.net/elclanrs/t8G2q/4/
不确定我明白你在寻找什么,但这里有一个写得不好的例子,只是为了展示如何通过迭代来做这样的事情:
有一个 在这里摆弄来尝试一下吗?
Not sure I get what your looking for, but here's a poorly written example done in a hurry to just show how you can do something like this with iteration:
There's a fiddle here for trying it?
@minh,如果您只是想检查是否只有一个“,”或“。”在输入的数字中,您可以简单地执行以下操作:
val.match(/^\d+([.,]\d+)?$/)
我可能完全误解了您想要检查的内容。如果是这样,请发表评论。
@minh, if you simply want to check that there is only one "," or "." in the inputted number, you can simply do something like:
val.match(/^\d+([.,]\d+)?$/)
I may have misunderstood exactly what you want to check for. If so, please post a comment.