validate that you have typed numbers and points

0

How to check if a user typed in one of several input of the same class, only characters characters from 0 to 9 and points ... not allowing me to only type those values if not typing validate with the event Keyup

$(document).ready(function()
{
  $('.input_valores_provisionales').keyup(
    function()
    { 
      $(".input_valores_provisionales").each(
        function()
        {

        }
      );
    }
  );
}
    
asked by Gabriel Uribe Gomez 06.04.2018 в 23:05
source

3 answers

1

$(".input_valores_provisionales").keypress(function(event) {
  var c = event.keyCode || event.which,
  key = String.fromCharCode(c),
  chars = "0123456789."
  return chars.search(key) !== -1
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

With the event keypress you can handle it better and with the properties keyCode or which of the event (depends on the browser) together with the function String.fromCharCode .

I advise you to gather in a string all the allowed characters and check if the entered character is in that string. Returning false in the method keydown() blocks the write action.

    
answered by 06.04.2018 в 23:12
0

You can validate a input to only accept numeric values with the event onkeydown this allows you to accept or reject the value before it is loaded to input .

Here is a functional example of how to do it.

Example:

$(document).ready(function() {
 $('.isNumber').on("keydown", function(e) {
   if(e.key.length == 1 && e.key != "." && isNaN(e.key))
    return false;
 });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="isNumber">
    
answered by 07.04.2018 в 15:34
-1

I leave the code:

<input type="text" class="input-number" value="" />

javascript:

$('.input-number').on('input', function () { 
    this.value = this.value.replace(/[^0-9]/g,'');
});

you can adapt it to your needs luck!

    
answered by 06.04.2018 в 23:11