How i can add comma after each entered number only

mohammad sarvar :

How can i add comma automatically after each entered number. For example like : 1,2,3,4

If i add number 5 after 4 so it should be separated with the comma. Below is my code.

function addComma(txt) {
    txt.value = txt.value.replace(",", "").replace(/(\d+)(\d{3})/, "$1,$2");
}
<input type="text" id="txt" onkeyup="addComma(this);" />

the comma is separating after a few numbers. I require an auto-add comma after each number has been entered.

Shree :

I change your RegEx. See below example.

function addComma(txt) {
    txt.value = txt.value.replace(/(\d)(?=(\d)+(?!\d))/g, "$1,");
}
<input type="text" id="txt" onkeyup="addComma(this);" />

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=29640&siteId=1