How to check if an input has a string?

Asked

Viewed 1,210 times

4

I need to check if the user typed a string in a field where he can only receive an Integer, did the code sequinte and when I click on the button, he always returns the Alert regardless of whether I write an integer or a string, which is 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;
  } 
}); 
  • 1

    Tip: Make an Alert after the first one that shows typeof endereco.

4 answers

3

You can check with a regular expression and the method 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>
<input type="text" id="denuncias_end">
    
    <button class="nextButton">Next</button>

For this particular case, this option is more viable than the $.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

At @Qmechanic73’s suggestion to use .test() as an alternative to .match() for this case. See here the reason

  • 1

    Good answer! I would only change the .match() for .test(), because you just want to know if the string whether or not it contains numbers, I even posted something about it here.

  • 1

    @Qmechanic73, you’re right, in this case, there’s no reason to use match() and yes test(). I edited adding your suggestion.

  • This way worked well as expected, I tested with strings and integers. But when I click on save which is my nextButton button, it pops up the Alert with the message, but allows saving. I answered my own question with the code I made, look at that...

2

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

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

$("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 cited in that Stackoverflow response, can also be used jQuery.isNumeric to define whether a String is a number.

  • Oops, this way it worked here too, but when I click on the save button my nextButton there, it appears the Alert and allows saving. Look how I did in answering the question.

  • I don’t understand the problem.

1

This is how I did it:

By pressing the keys on input it already checks if 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;
    }

});

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

That should work well.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.