thousand separator
-
how to make any filed as thousand separators. it must auto change the numbers while user inserts into it. A comma should be added after every 3 digits.
*for example – when user input 10000, it auto-change to 10,000
I found some solution (check below)
BUT I WANT TO KNOW HOW TO INTEGRATE THIS CODES INTO WPFORMS CODEthis are few solution –
solution – 1
____________________myNumber.value = commify(myNumber.value)
myNumber.addEventListener(“change”, function(){
commify(event.target.value)
})function commify(value){
var chars = value.split(“”).reverse()
var withCommas = []
for(var i = 1; i <= chars.length; i++ ){
withCommas.push(chars[i-1])
if(i%3==0 && i != chars.length ){
withCommas.push(“,”)
}
}
var val = withCommas.reverse().join(“”)
myNumber.parentNode.setAttribute(“comma-value”,val)
}input field –
<input type=”number” id=’myNumber’ value=”40000″ step=’100′>solution – 2
____________________function addCommas(nStr)
{
nStr += ”;
x = nStr.split(‘.’);
x1 = x[0];
x2 = x.length > 1 ? ‘.’ + x[1] : ”;
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, ‘$1’ + ‘,’ + ‘$2’);
}
return x1 + x2;
}input field –
<input type=”text” id=”txtBox” onchange=”return addCommas(this.value)” />
- The topic ‘thousand separator’ is closed to new replies.