Format a textbox like "0.00"
H开发者_Go百科ow to format a textbox like "0.00" using jQuery or JavaScript? If I enter value 59
, then it should become 59.00
. If I enter value as 59.20
, then it should be the same.
With jQuery, assuming you have an input like this:
<input type="text" id="someInput" name="something"/>
You can use this:
$('#someInput').blur(function() {
var floatValue = parseFloat(this.value);
if (!isNaN(floatValue)) { // make sure they actually entered a number
this.value = floatValue.toFixed(2);
}
});
Whenever the input loses focus, the value will be converted always have 2 decimal places, as long as the user entered something that can be parsed into a number.
精彩评论