2

parseInt("123@231.23") returns 123, which is a number.

There are tons of functions out there to detect if something is a number or not already, but they all depend on parseInt.

What is another generic way of detecting that this is not an integer without using regex?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Sherman Szeto
  • 2,325
  • 4
  • 18
  • 26

5 Answers5

3
if (isNaN("123@231.23"))
{
 alert("IsNaN - not a number");
}
else
{
 alert ("it is a number");
}

I'm assuming that OP need to distinguish if input is a number or not. If input is float or integer looks irrelevant to his problem. Maybe, I'm wrong...

EDIT: Alright, to keep everyone happy, integer in javasript is pretty big. How big integer is in javascript check here.

Asking if something is integer is asking is it a whole number between 9007199254740992 and -9007199254740992. Wholeness of the number you may check using modulus %

$("#cmd").click(function (e) { ChectIfInteger( $("#txt").val() ) });

function ChectIfInteger(myval){

  if (isNaN(myval)){ 
    alert("not integer (not number)")   
  }
  else{
  
    //it is a number but it is integer?
    if( myval % 1 == 0 ){
    
      if (myval <= 9007199254740992 && myval >= -9007199254740992)
        {
          alert("it is integer in javascript");
        }
      else{
          alert ("not integer");
      }
    }
    else{
      alert("nope, not integer");
    }
    
    
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" id="txt"/>
<input type="button" id="cmd" value="test input">
Community
  • 1
  • 1
dllhell
  • 1,987
  • 3
  • 34
  • 51
  • `isNaN("123")` returns false – NeoWang Jul 26 '15 at 14:30
  • 3
    Because `isNan` returns true if the arg is *not* a number. 123 is a number so it returns false. – Dan Lowe Jul 26 '15 at 14:32
  • @NeoWang "**is** **N**ot **A** **N**umber" so false means it is a number in this case... – epascarello Jul 26 '15 at 14:33
  • `isNaN(" "): false` `isNaN(false): false` `isNaN(true): false` – pdenes Jul 26 '15 at 14:35
  • 1
    Well `isNaN` is not really safe unless you know what you're testing. In this case it's fine, but it's not really what `isNaN` means. – Pointy Jul 26 '15 at 14:35
  • 1
    The question is asking about integers, `isNaN()` doesn't really do that. And it's best to avoid the global `isNaN()` due to the confusing special cases. – light Jul 26 '15 at 14:49
  • 2
    *This anser is wrong*, this just check numbers, not integer numbers, for example `isNaN(123231.23)` will produce `true` cause it is a number, but should produce `false ` since it is not an integer number!!!! – BAD_SEED Jul 26 '15 at 14:50
  • I'm assuming that OP need to distinguish is input a number or not. Is input float or integer looks irrelevant to his question. Maybe, I'm wrong... – dllhell Jul 26 '15 at 14:51
  • @dllhell: `isNaN()` doesn't work that way: it checks if a value is `NaN` and also does some auto-coercion for its input. But this does _not_ mean that if `isNaN(x) == false` then `x` must be a number! – pdenes Jul 26 '15 at 14:59
  • @dllhell maybe you should re-read the title of the question :) – Pointy Jul 26 '15 at 14:59
  • Please provide better solution – dllhell Jul 26 '15 at 15:02
  • *You* should provide a better solution, not us. You should remove your answer, or at least edit it with solutions provided by other answer... – BAD_SEED Jul 27 '15 at 10:11
  • My solution is adequate for OP's problem and it's acceptance is a proof. Otherwise, OP should unaccept this answer and accept other one. – dllhell Jul 27 '15 at 10:33
  • The question was: What is another generic way of detecting that this **is not an integer**? Your solution provide a valid answer for `123@231.23`, that's why the OP accepted it. – BAD_SEED Jul 27 '15 at 12:40
1

Convert back to String and compare:

String(parseInt("123"))=="123" // true
String(parseInt("123.sdfs"))=="123.sdfs" //false
NeoWang
  • 17,361
  • 24
  • 78
  • 126
1

If you really want to check "for valid integer" you must combine isNaN with something else like this:

function isValidInteger(numberToTest) {
  return !(isNaN(numberToTest) || String(parseInt(numberToTest)) !== numberToTest.toString());    
}

This will evaluate like:

console.log(isValidInteger('123@231.23')); // false
console.log(isValidInteger('123231.23')); // false
console.log(isValidInteger('12323')); // true
console.log(isValidInteger(1e-1)); // false
console.log(isValidInteger('1e-1')); // false

And this work even with numbers. Here is PLNKR to test.

BAD_SEED
  • 4,840
  • 11
  • 53
  • 110
0

I think this is the best way to test for integers:

function isInt(str) {
    if (typeof str !== 'number' && typeof str !== 'string') {
        return false;
    }

    return str % 1 === 0;
}

Just note that strings / numbers like "123.0" evaluates to true.

light
  • 4,157
  • 3
  • 25
  • 38
0

Here's yet another one that doesn't rely on string stuff:

function looksLikeInteger(n) {
  return +n == n && +n === ~~n;
}

Probably should be called "looksLikeJavaScriptInteger" because it only works for 32-bit integers. It coerces to numeric with unary + and then checks for equality (so ugly strings and objects are tossed out there) and then checks to make sure that the numeric value doesn't change when coerced to an integer.

Pointy
  • 405,095
  • 59
  • 585
  • 614