jquery allow only numbers and decimal

jquery allow only numbers and decimal : here you can learn to How to allow numeric (0-9) & decimal value textbox using jQuery?.

jquery allow only numbers and decimal

jQuery Allow Numbers and Decimal only in Textbox [numeric]. Steps to allow only numbers and dot (decimal/ float).

$(function () {
        $("input[id*='txtQty']").keydown(function (e) {


            if (e.shiftKey == true) {
                e.preventDefault();
            }

            if ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105) || e.keyCode == 8 || e.keyCode == 9 || e.keyCode == 37 || e.keyCode == 39 || e.keyCode == 46 || e.keyCode == 190) {

            } else {
                e.preventDefault();
            }
            
            if($(this).val().indexOf('.') !== -1 && e.keyCode == 190)
                e.preventDefault();

        });
    });

Jquery – Allow Only Numbers and 2 Decimal in Textbox Example

index.html

<!DOCTYPE html>
<html>
<head>
    <title>Jquery Only Allow Input Float Number - www.pakainfo.com</title>
    <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
  
<div class="container">
    <h2>Jquery Only Allow Input Float Number - www.pakainfo.com</h2>
    <input type="text" name="number" class="number" autocomplete="off">
</div>
  
</body>
  
<script type="text/javascript">
    $('.number').keypress(function(e) {
      if ((e.which != 46 || $(this).val().indexOf('.') != -1) &&
        ((e.which < 48 || e.which > 57) &&
          (e.which != 0 && e.which != 8))) {
        e.preventDefault();
      }
  
      var text = $(this).val();
  
      if ((text.indexOf('.') != -1) &&
        (text.substring(text.indexOf('.')).length > 2) &&
        (e.which != 0 && e.which != 8) &&
        ($(this)[0].selectionStart >= text.length - 2)) {
        e.preventDefault();
      }
    });
</script>
</html>

Example 2: Jquery Only Allow Input Float Number

index.html

<!DOCTYPE html>
<html>
<head>
    <title>Jquery Only Allow Input Float Number - www.pakainfo.com</title>
    <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
  
<div class="container">
    <h2>Jquery Only Allow Input Float Number - www.pakainfo.com</h2>
    <input type="text" name="number" class="number" autocomplete="off">
</div>
  
</body>
  
<script type="text/javascript">
    $('.number').keypress(function(event) {
      if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) {
        event.preventDefault();
      }
    });
</script>
</html>

Don’t Miss : JQuery Allow Only Numbers Input In Textbox

I hope you get an idea about jquery allow only numbers and decimal.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.