9

How can I simply check if string can be converted to integer?

And I mean only integer, so string like '10.1' would not be converted to 10.

For example if I do:

parseInt('10.1', 10)

It returns 10

Is there something equivalent like it is in Python, so I would not need to do lots of checks?

In Python if I do int('10.1'), it gives me error, but if string is actually integer, only then it lets you convert it like int('10'). So it is very easy to check that without even using regex or some additional checks.

I tried this:

    function isInteger (value) {
        if ($.isNumeric(value) && Number(value) % 1 === 0){
            return true;
        } else {
            return false;
        }
    }

It kinda works, but if I write for example 10., it will return true, because Number converts to 10. I guess I need to use regex to check such thing?

P.S. I searched for this question, but all answers seem to be vague and actually does not answer how to properly check for integer (not just if it is number).

Andrius
  • 19,658
  • 37
  • 143
  • 243
  • http://www.w3schools.com/jsref/jsref_parseint.asp – HudsonPH Jun 07 '16 at 08:26
  • [parseInt](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/parseInt) possibly and an [isNaN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN) check? `parseInt` returns `NaN` if it can't parse. – ste2425 Jun 07 '16 at 08:26
  • Possible duplicate of [(Built-in) way in JavaScript to check if a string is a valid number](https://stackoverflow.com/questions/175739/built-in-way-in-javascript-to-check-if-a-string-is-a-valid-number) – NayoR Sep 25 '19 at 22:22

4 Answers4

21

One possibility is to use a regex to test, like so:

function isInteger(value) {
  return /^\d+$/.test(value);
}
Anduril
  • 1,236
  • 1
  • 9
  • 33
5

If i understand your question correctly then this should do it:

function isInteger(value) {
  if(parseInt(value,10).toString()===value) {
    return true
  }
  return false;
}

It converts a string to an integer and then back to a string and compares that to the original string. If they match, then it returns true, else it returns false.

Emil S. Jørgensen
  • 6,216
  • 1
  • 15
  • 28
  • 1
    @PeeHaa I see, but thats what I needed, because I only needed to be true for strings like '123' and if it would contain any non integer symbol (even if it represents hexadecimal), it should be false. I guess more specific way would be to use `parseInt(val, 10)`, to show that I only care for decimals. – Andrius Jun 07 '16 at 08:46
  • What is what you needed? Different returns for different implementations for JS vms? – PeeHaa Jun 07 '16 at 08:48
  • Why isn't it needed? Enlighten me please? The page clearly tells us that when not defining the the radix the different implementation basically just do something and worse might / probably will just do something different than others. – PeeHaa Jun 07 '16 at 08:50
  • As long as it is a decimal formatted string coming in, then `parseInt` will default to a radix of `10`. Only JavaScript older than the [Ecmascript 5](http://www.w3schools.com/jsref/jsref_parseint.asp) (from before 2009!) might have a problem, and then only with leading `0`. I agree it is safer but not necessarily necessary. – Emil S. Jørgensen Jun 07 '16 at 08:58
0

You can use regex to detect if it match any value.

function isInterger(value) {
     if (value.match(/^\d+$/) {
        return true;
   } else {
        return false;
   }
}

https://regex101.com/r/lE5tK1/1

Radonirina Maminiaina
  • 6,958
  • 4
  • 33
  • 60
0

You can use

Number function of js (http://www.w3schools.com/jsref/jsref_number.asp)

or

parseInt function of js (http://www.w3schools.com/jsref/jsref_parseint.asp)

Gaurav joshi
  • 1,743
  • 1
  • 14
  • 28