
Originally Posted by
sapator
Edit.Sorry i forgot this.I would like, if possible the comma to appear at the first keyup so when someone writes 1 it would be ,01 when it presses 1 again would be ,11 then again would be 1,11 and again 11,11 etc.
well that complicates matters but check this out
Code:
<script type="text/javascript">
function formatNumber(txt){
var input = txt.value
var lgth = input.length
var lastDigit = input.charAt(lgth-1)
if (isNaN(lastDigit)){
//char is not a number
txt.value = input.substr(0,lgth-1)
alert("Numbers Only")
}else{
//format text
num = input.replace(",", "") //remove comma
if (num.substr(0,1) == 0){
num = num.substr(1,lgth-1) //remove 0 from start
}
if (num.length == 0){
//enterd 0 as first char
txt.value = ""
//alert("Can't start with 0")
}else if (num.length == 1){
txt.value = ",0" + num
}else if (num.length == 2){
txt.value = "," + num
}else{
last2Digits = num.substr(lgth-3,lgth-1)
firstDigits = num.substr(0,lgth-3)
txt.value = firstDigits + "," + last2Digits
}
}
}
</script>
on the textbox add onkeyup="formatNumber(this)"