1

I have to define an input text which accepts only integer numbers.

I tried with a regular expressions but the decimal part of the value is still visible.

I used this function:

$(document).on("input","input", function(e) {
    this.value = this.value.replace(/[^\d\\-]/g,'');
})
Matteo Baldi
  • 5,613
  • 10
  • 39
  • 51
durga
  • 11
  • 1
  • 4
  • why not use type number? – guradio Oct 26 '16 at 07:56
  • but it is also accepting decimal(.) in input, I didn't required (.) in input. – durga Oct 26 '16 at 07:58
  • Possible duplicate of [How to allow only numeric (0-9) in HTML inputbox using jQuery?](https://stackoverflow.com/questions/995183/how-to-allow-only-numeric-0-9-in-html-inputbox-using-jquery) – emkey08 Jul 18 '19 at 08:11

4 Answers4

3

How about this one?

$('input').on('input blur paste', function(){
 $(this).val($(this).val().replace(/\D/g, ''))
})
<input>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
Sean W
  • 527
  • 6
  • 15
1

Simple and easy to use. Try it: edited: also call in onblur event to prevent paste.

function numOnly(selector){
  selector.value = selector.value.replace(/[^0-9]/g,'');
}
<input type="text" onkeyup="numOnly(this)" onblur="numOnly(this)">
AHJeebon
  • 1,218
  • 1
  • 12
  • 17
0

You can detect on keydown if is numeric and you can check on paste event if is numeric and remove all non-numeric. This remove dot too.

Original source : keydown detect : Paste remove

This code below has been modified from the original source.

Please try:

$( "#input" ).on("paste", function() {  
    setTimeout(function(){
    if ($('#input').val() != $('#input').val().replace(/\D/g,"")) 
    { 
      $('#input').val($('#input').val().replace(/\D/g,""));
    }
    },10)
});

$('#firstrow').on('keydown', '#input', function(e){-1!==$.inArray(e.keyCode,[46,8,9,27,13])||/65|67|86|88/.test(e.keyCode)&&(!0===e.ctrlKey||!0===e.metaKey)||35<=e.keyCode&&40>=e.keyCode||(e.shiftKey||48>e.keyCode||57<e.keyCode)&&(96>e.keyCode||105<e.keyCode)&&e.preventDefault()});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="firstrow">
  <input id="input" type="text">
</div>
Community
  • 1
  • 1
P. Frank
  • 5,691
  • 6
  • 22
  • 50
0
Finally I solved the above issue with my requirement to type only number is

$(document).on("input", ".skilltxt", function(e) {
                this.value = this.value.replace(/[^\d]/g,'');
             });
             $(".skilltxt").on('paste',function(e) {

                    if(gBrowser=='IE') {
                        var clipboardData, pastedData;
                         clipboardData = e.clipboardData || window.clipboardData;
                          pastedData = clipboardData.getData('Text');

                    }
                    else if((gBrowser=='Firefox')|| (gBrowser=='Chrome')){ 
                          var pastedData = (e.originalEvent || e).clipboardData.getData('text/plain');
                            window.document.execCommand('insertText', false, pastedData)
                    }
                if(Math.floor(pastedData) == pastedData && $.isNumeric(pastedData)){
                   $(this).val($(this).val().replace(/[^\d]/g,''));
                  }
                  else{
                   return false;
                  }
             });
durga
  • 11
  • 1
  • 4