R J.
R J.

Reputation: 1542

Allow only 2 decimal points entry to a textbox using jquery?

Allow only 2 decimal points when entering number to a textbox using jquery.

Please suggest any regex to allow only two decimal in textbox.

I have tried the following code.

$("#amountId").val().replace (/(\.\d\d)\d+|([\d.]*)[^\d.]/, '$1$2');

Upvotes: 10

Views: 48189

Answers (7)

Ravi Sharma
Ravi Sharma

Reputation: 370

function isNumberKey(evt, element) { var charCode = (evt.which) ? evt.which : event.keyCode if (charCode > 31 && (charCode < 48 || charCode > 57) && !(charCode == 46 || charCode == 8)) return false; else { var len = $(element).val().length; var index = $(element).val().indexOf('.'); if (index > 0 && charCode == 46) { return false; } if (index > 0) { var CharAfterdot = (len + 1) - index; if (CharAfterdot > 3) { return false; } }

        }
        return true;
    }

Upvotes: 0

Sonu Chohan
Sonu Chohan

Reputation: 273

  $('#remittance_amt').keyup(function () {
      if (this.value != this.value.replace(/[^0-9\.]/g, '')) {
        this.value = this.value.replace(/[^0-9\.]/g, '');
      }
  });

Upvotes: 0

Devraj Gadhavi
Devraj Gadhavi

Reputation: 3611

Another working solution

Html

<input type="text" id="myTextBox" class="two-decimals">

jQuery

$(".two-decimals").on("keypress", function (evt) {
        var $txtBox = $(this);
        var charCode = (evt.which) ? evt.which : evt.keyCode
        if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46)
            return false;
        else {
            var len = $txtBox.val().length;
            var index = $txtBox.val().indexOf('.');
            if (index > 0 && charCode == 46) {
              return false;
            }
            if (index > 0) {
                var charAfterdot = (len + 1) - index;
                if (charAfterdot > 3) {
                    return false;
                }
            }
        }
        return $txtBox; //for chaining
    });

Upvotes: 6

Vishal Surjuse
Vishal Surjuse

Reputation: 170

HTML:

<input type="text" class="maskedExt" maskedFormat="3,2" />
 maskedFormat="number count before decimal point, number count after decimal point"

Script:

$(document).ready(function () {
    $('body').on('keyup', '.maskedExt', function () {
        var num = $(this).attr("maskedFormat").toString().split(',');
        var regex = new RegExp("^\\d{0," + num[0] + "}(\\.\\d{0," + num[1] + "})?$");
        if (!regex.test(this.value)) {
            this.value = this.value.substring(0, this.value.length - 1);
        }
    });
});

Upvotes: 5

Mr_Green
Mr_Green

Reputation: 41832

I was just testing using regex to learn it. But I recommend going with roasted's solution.

<input id="txtId" type="text"></input>

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 = "";
        }
    }
}

Working Fiddle

Upvotes: 7

Snehal Chavan
Snehal Chavan

Reputation: 1

function checkDecimal(el){
 var ex = /^[0-9]+\.?[0-9]*$/;
  if(ex.test(el.value)==false){
   el.value = el.value.substring(0,el.value.length - 1);
  }
}

Upvotes: 0

A. Wolff
A. Wolff

Reputation: 74420

You could do it without regex:

var dec = parseFloat($("#amountId").val(),10).toFixed(2);

Upvotes: 13

Related Questions