1

I have one textbox.It should be allow only decimal numbers and after dot only allow two digit(example 34545.43). how we can do it using jquery i have searched in google and stackoverflow but not satisfied answer because some script is not working in chrome and firefox. I tried but it is not working properly.So need help how to do it.http://jsfiddle.net/S9G8C/1685/

Js:

$('.allow_decimal').keyup(function (evt) {
    var self = $(this);
    self.val(self.val().replace(/[^0-9\.]/g, ''));
    if ((evt.which != 46 || self.val().indexOf('.') != -1) && (evt.which < 48 || evt.which > 57)) {
        evt.preventDefault();
    }
});
5
  • 1
    maybe you should make search before asking question. stackoverflow.com/questions/23039374/… Commented Feb 28, 2018 at 8:12
  • Harun:-That questions is different Commented Feb 28, 2018 at 8:36
  • @HarunKARATAŞ that question is different than this one. Commented Feb 28, 2018 at 8:39
  • I need same like this::jsfiddle.net/jamseernj/6guy3sp9 Commented Feb 28, 2018 at 8:40
  • @snkranya and NikhilSavaliya I'm sorry i just realized question is different. Commented Feb 28, 2018 at 8:41

1 Answer 1

2

This jQuery function will round the value on blur event of textbox

$.fn.getNum = function() {
    var val = $.trim($(this).val());
    if(val.indexOf(',') > -1) {
        val = val.replace(',', '.');
    }
    var num = parseFloat(val);
    var num = num.toFixed(2);
    if(isNaN(num)) {
        num = '';
    }
    return num;
}
$(function() { //This function will work on onblur event 
    $('#txt').blur(function() {
        $(this).val($(this).getNum());
    });

});
Number: <input type="text" id="txt" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

You can directly remove the 3rd digit when the user enters that.

    var txt = document.getElementById('txtId');
    txt.addEventListener('keyup', myFunc);
    
    function myFunc(e) {
        var val = this.value;
        var re = /^([0-9]+[\.]?[0-9]?[0-9]?|[0-9]+)$/g;
        var re1 = /^([0-9]+[\.]?[0-9]?[0-9]?|[0-9]+)/g;
        if (re.test(val)) {
            //do something here
    
        } else {
            val = re1.exec(val);
            if (val) {
                this.value = val[0];
            } else {
                this.value = "";
            }
        }
    }
    <input id="txtId" type="text"></input>

Sign up to request clarification or add additional context in comments.

6 Comments

I need same like this::jsfiddle.net/jamseernj/6guy3sp9
@snkranya Ok Now I updated with another jquery function that will allow a user to enter values but when user leave that text box it will automatically round that numbers in Two digits.
can u send by jsfiddle?
pls do not send dublicate answer
any two codes can match, can you tell me what should exactly happen after the user enter a 3rd digit or user should not be able to enter 3rd digit
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.