简体   繁体   中英

Format value entered in input text box

I have one input text box for amount .I want to format user entered value . If user enter value 8 then it should display it as 0.08 if user enter 89 it should display 0.89. and so on and for 0 it should display 0.00

**********EDIT************************ I want to calculate on keyup (when user type inputs)

You can add a javascript method and call it on onblur of field, which will calculate and reset value as following:

HTML:

<input type="text" id="pointValue" onBlur="calculateFractionPoint(this);" />

JS:

function calculateFractionPoint(obj){
    var value = parseInt(obj.value);

    if(isNaN(value) && !isFinite(value)){
        alert("Number is not valid!");
        obj.value = "";
        return;
    }

    if(value === 0)
        obj.value = "0.00";
    else
      obj.value = parseFloat(value).toFixed(2) / 100;   
}

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM