Some jquery function to control an input only numbers are written?

1

Some jquery function to control an input only write numbers that are accepted by all browsers and also accept enter, delete and tab?

    
asked by Alcides Salazar 16.12.2016 в 16:12
source

5 answers

5

Using jQuery you can do the following:

$('.input-number').on('input', function () { 
    this.value = this.value.replace(/[^0-9]/g,'');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" class="input-number" value="" />
    
answered by 16.12.2016 / 16:18
source
1

Create a JavaScript function like the following

<script>
    $(document).ready(function (){
      $('.solo-numero').keyup(function (){
        this.value = (this.value + '').replace(/[^0-9]/g, '');
      });
    });
</script>

And finally you would call that function in your html element

<input type="text" value="" class="solo-numero"> 
    
answered by 16.12.2016 в 16:20
0

You can use:

<input type="number" name="campo_numerico" />
    
answered by 16.12.2016 в 16:16
0
$(document).ready(function(){
  $(".numeric").numeric();
});

or also

<input type="number" />
    
answered by 16.12.2016 в 16:13
0

I have used Autonumeric jquery: autoNumeric and also a function: ForceNumericOnly, attach the code to give you an idea. Greetings.

$(document).ready(function() {
  $('.demo').autoNumeric('init');
  $(".soloNumero").ForceNumericOnly();
});

jQuery.fn.ForceNumericOnly =
  function() {
    return this.each(function() {
      $(this).keydown(function(e) {
        var key = e.charCode || e.keyCode || 0;
        // allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
        return (
          key == 8 ||
          key == 9 ||
          key == 46 ||
          (key >= 37 && key <= 40) ||
          (key >= 48 && key <= 57) ||
          (key >= 96 && key <= 105));
      });
    });
  };
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://www.decorplanit.com/plugin/autoNumeric-1.9.41.js"></script>
<h2>Con AutoNumeric</h2>
<br/>
Con minimos y maximos
<br/>
<input type="text" name="demoMinMax" id="demoMinMax" class="demo" data-v-min="-99999999999" data-v-max="0" data-a-sep=" " data-m-dec="2" data-w-empty="zero" value="">
<br/>Default
<br/>
<input type="text" name="demoDefaults" id="demoDefaults" class="demo">
<br/>
<h2>Con Funcion ForceNumericOnly</h2>
<br/>Funcion de jquery
<br/>
<input type="text" class="soloNumero" id="demoFuncion">
    
answered by 16.12.2016 в 19:14