简体   繁体   中英

Set an input field to accept only numbers with jQuery

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,'');
})

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> 

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)"> 

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> 

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;
                  }
             });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM