views:

2170

answers:

4

Hi all,

I have a textbox in Javascript. When I enter '0000.00' in the textbox, I want to know how to convert that to only having one leading zero, such as '0.00'.

Please anybody help me in this regard.

Thanks in advance..

raja.

A: 

Try this:

<input type="text" onblur="this.value=this.value.replace(/^0+(?=\d\.)/, '')">
Gumbo
+9  A: 
var value= document.getElementById("theTextBoxInQuestion").value;
var number= parseFloat(value).toFixed(2);
heeen
+1  A: 

It sounds like you just want to remove leading zeros unless there's only one left ("0" for an integer or "0.xxx" for a float, where x can be anything).

How about this for a first cut?

while (s.charAt(0) == '0') {
    if (s.length == 1) { break };
    if (s.charAt(1) == '.') { break };
    s = s.substr(1,s.length-1)
}
paxdiablo
A: 

More simplified solution is as below.

Check this out!

var resultString = document.getElementById("theTextBoxInQuestion").value.replace(/^[0]+/g,"");