hemiz
hemiz

Reputation: 91

how to add automatic decimel for a number in input field while typing

im trying to make input that automaticaly change to decimel while typing number but fixed with 2 decimel point only. please help me , because im still new with jquery

my code :

 $.fn.billFormat = function() {
        $(this).keyup( function( e ){
                if( isNaN(parseFloat(this.value )) ) return;
                this.value = parseFloat(this.value).toFixed(2);
        });
        return this; 
 }

$('#ENQUIRY_PREPAIDBILL .bill_issued').billFormat();

i want the output to be like this

0.00 <------ default value
0.01 <------ typing 1
0.10 <------ typing 0
1.00 <------ typing 0

Upvotes: 0

Views: 483

Answers (2)

Nir
Nir

Reputation: 356

here is another solution that will not worry about overflow (you can find the demo here

$(document).ready(function(){
    $('#txtInput').keydown(function(e){
        var value = $('#txtInput').val().toString();
        var number = null;
        if(value == '')
            value = '0.00';
        switch(e.which)
        {
            case 48:
            case 49:
            case 50:
            case 51:
            case 52:
            case 53:
            case 54:
            case 55:
            case 56:
            case 57:
                number = e.which - 48;
            case 8:
            case 46:
            case 32:
            case 37:
            case 38:
            case 39:
            case 40:
                break;
            default:
                e.preventDefault();
                break;
        }
        var dotIndex = value.indexOf('.');
        if(number === null)
        {
            value = value.substr(0,dotIndex-1) + '.' + value.substr(dotIndex -1,1) + value.substr(dotIndex+1,1);
            if(value.indexOf('.') ==0)
            {
                value = '0' + value;
            }
        }
        else
        {

            value = value.substr(0,dotIndex) + value.substr(dotIndex+1,1) + '.' + value.substr(dotIndex+2);

            value += number.toString();  
            value = value.replace(/^0+/,'');
            if(value.indexOf('.') == 0)
            {
                value = '0' + value;
            }               

        }
        $('#txtInput').val(value);
        e.preventDefault();
    }).keyup(function(e){

        switch(e.which)
        {
            case 48:
            case 49:
            case 50:
            case 51:
            case 52:
            case 53:
            case 54:
            case 55:
            case 56:
            case 57:
                e.preventDefault();
                break;
        }
    });
});​

and the html for this code

<input type='text' id='txtInput' />​

Upvotes: 0

xdazz
xdazz

Reputation: 160863

 $.fn.billFormat = function() {
        this.val("0.00");
        this.keyup( function( e ){
                if( isNaN(parseFloat(this.value )) ) return;
                this.value = parseFloat(this.value * 10, 10).toFixed(2);
        });
        return this; 
 }

Here is the demo.

Upvotes: 2

Related Questions