How to check if an input has a String?

4

I need to check if the user typed a string in a field where they can only receive an Integer, I made the sequential code and when I click the button, it always returns the alert regardless of whether I write an integer or a string, what's wrong?

$("button.nextButton").unbind("click").click(function(e){ 
  e.preventDefault();

  var endereco = $("#denuncias_end").val();
  if(typeof endereco == 'number') 
  {
    return true;
  }
  else
  {
    alert("Por favor, numero de endereco somente em numeros.");
    return false;
  } 
}); 
    
asked by anonymous 02.03.2015 / 18:41

4 answers

3

You can check with a regular expression and match , see:

$("button.nextButton").unbind("click").click(function(e){ 
  e.preventDefault();

  var endereco = $("#denuncias_end").val();
  
  //if(endereco.match(/^\d+$/)) // retorna um array se verdadeiro ou nulo se falso
  if(/^\d+$/.test(endereco)) // retorna verdadeiro ou falso
  {
    return true;
  }
  else
  {
    alert("Por favor, numero de endereco somente em numeros.");
    return false;
  } 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype="text" id="denuncias_end">
    
    <button class="nextButton">Next</button>

For this particular case, this option is more feasible than $ .isNumeric , note that:

$.isNumeric( "-10" );     // true
$.isNumeric( 16 );        // true
$.isNumeric( 0xFF );      // true
$.isNumeric( "0xFF" );    // true
$.isNumeric( "8e5" );     // true (exponential notation string)
$.isNumeric( 3.1415 );    // true
$.isNumeric( +10 );       // true
$.isNumeric( 0144 );      // true (octal integer literal)
$.isNumeric( "" );        // false
$.isNumeric({});          // false (empty object)
$.isNumeric( NaN );       // false
$.isNumeric( null );      // false
$.isNumeric( true );      // false
$.isNumeric( Infinity );  // false
$.isNumeric( undefined ); // false

EDITED

As suggested by @ Qmechanic73 use .test() instead of .match() for this case. See the reason here

    
02.03.2015 / 20:38
2

The problem is that endereco in your code will always be String , because jQuery.val() will always return String to an input or a textarea.

To do the check you want, you need to use parseInt , which will return NaN if it can not parse an integer in its input. You can use the isNaN function to know whether a variable is NaN . Your updated code would look like:

$("button.nextButton").unbind("click").click(function(e){ 
  e.preventDefault();

  var endereco = parseInt($("#denuncias_end").val());
  if(!isNaN(endereco)) 
  {
    return true;
  }
  else
  {
    alert("Por favor, numero de endereco somente em numeros.");
    return false;
  } 
}); 

As quoted in this StackOverflow response , you can also use jQuery.isNumeric to define whether a String is a number.

    
02.03.2015 / 18:59
1

I was able to do the following:

By pressing the input keys it already checks whether they are numbers or strings according to the ASCII table.

$("#denuncias_end").keypress(function (e) {
    if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
       alert("Digite somente Numeros");
       $("#denuncias_end").focus();
       return false;
    }

});
    
03.03.2015 / 20:16
1

Do the following:

$(document).on("click", "button.nextButton", function(e){ 
    e.preventDefault();

    var endereco = parseInt($("#denuncias_end").val());
    var regex = new RegExp(/^-?\d*\.?\d*$/);
    if(regex.test(endereco)) 
    {
        return true;
    }
    else
    {
        alert("Por favor, numero de endereco somente em numeros.");
        return false;
    } 
});

This should work fine.

    
03.03.2015 / 21:24