简体   繁体   English

如果数字包含5或6位数字,请添加逗号

[英]Add comma if numbers contains 5 or 6 digits

My input field contains multiple numbers and every numbers contains 5 or 6 digts like this: 我的输入字段包含多个数字,每个数字包含5或6个数字,如下所示:

10883 500643 10886 502814

and when user keypress and if user press space I want automatically add comma (,) between them. 当用户按下按键时,如果用户按下空格,我要在它们之间automatically添加逗号(,)。

How can i achieve this ?! 我怎样才能做到这一点? Can anyone please help me :) 谁能帮帮我吗 :)

This is my code so far, it will add comma only after each numbers which contains 5 digits : 到目前为止,这是我的代码,它将仅在每个包含5位数字的数字之后添加逗号:

JavaScript: JavaScript:

<script>
    <input name="CustomerNumber" id="CustomerNumber">

    $("#CustomerNumber").on('input', function () {

    $(this).val($(this).val().replace(/,/g, '').replace(/(\d{5}(?!$))/g, "$1,"));

    });
</script>

OutPut will be come like this user enter first 5 digits comma will automatic added than user enter 6 digits comma automatic added and so on : 用户输入前5位逗号将比该用户输入的前5位逗号自动添加输出,依此类推:

10883,500643,10886,502814

This will add a comma after minimum 5 and max 6 digits. 最小5位和最大6位后将添加一个逗号。 If number of digits are less than 5, it won't add any comma even if there's a space. 如果数字位数小于5,即使有空格也不会添加任何逗号。 And if they are more than 6 digits, it will automatically add a comma after every 6 digits. 如果它们超过6位数字,它将每6位数字后自动添加一个逗号。

$(this).val($(this).val().replace(/,/g, '').replace(/(\d{5,6})/g, "$1,")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM