如果用户之前输入了小数分隔符,如何不从键盘输入小数分隔符
我有插件,jQuery 格式编号。但这并不好。如果用户已经输入了小数分隔符,我希望它不输入小数分隔符。
示例:
122.222,555,,,,..
应变为122.222,555
122,222.555 ..,,,,
应变为122,222.555
这是我的插件:
(function($)
{
$.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);
});
});
};
})(jQuery);
这里是 html:
<input id="txt" size="40" type="text">
<input id="new" size="40" type="text">
<script type="text/javascript">
$('#txt').myPlugin();
$('#new').myPlugin({thousands:',',decimal:'.'});
</script>
I have plugin, jQuery format number. But it’s not good. I want it to not input the decimal separator if the user inputted the decimal separator already.
Examples:
122.222,555,,,,..
should become122.222,555
122,222.555 ..,,,,
should become122,222.555
Here’s my plugin:
(function($)
{
$.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);
});
});
};
})(jQuery);
and here html:
<input id="txt" size="40" type="text">
<input id="new" size="40" type="text">
<script type="text/javascript">
$('#txt').myPlugin();
$('#new').myPlugin({thousands:',',decimal:'.'});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许它可以帮助您知道有一个
.toLocaleString()
函数。Maybe it can help you to know there is a
.toLocaleString()
function.