I have a web page in which I input data into an <input />
, like 1000 so I want it to look like 1,000 but it does not change the ID value it just show for only becuase I am using same the ID in other calculation so if 1,000 then it will show NaN... how can I avoid this?
<input type="text" id="test" onBlur="addCommas()">
<script>
function addCommas(nStr){
var offset = nStr.length % 3;
if (offset == 0)
return nStr.substring(0, offset) + nStr.substring(offset).replace(/([0-9]{3})(?=[0-9]+)/g, "$1,");
else
return nStr.substring(0, offset) + nStr.substring(offset).replace(/([0-9]{3})/g, ",$1");
}
alert(addCommas("1234567"));
</script>
I have got this code from a forum... does anyone have an idea how to make this function simple so that it just shows in input box but when I get the ID value it shows 1000?