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).