6

what is the function which test if a string is an Integer or not like the method :

jQuery.isNumeric()

i want to test an input html element :

<input id='distance' type='text' />

thank you in advance

Bouraoui KACEM
  • 821
  • 1
  • 8
  • 20
  • possible duplicate of [Check a value is float or int in jquery](http://stackoverflow.com/questions/20311572/check-a-value-is-float-or-int-in-jquery) And this http://stackoverflow.com/questions/3885817/how-to-check-if-a-number-is-float-or-integer – Dan Sep 11 '14 at 13:37
  • That's the function. What else do you need to know? – Barmar Sep 11 '14 at 13:37
  • @Dan It's not a duplicate of that. That question is about deciding which it is, int or float. This question just wants to know if it's any kind ofnumber. – Barmar Sep 11 '14 at 13:38
  • Ok well there seem to be plenty of questions on this already - here's another similar one: http://stackoverflow.com/questions/600763/check-if-a-variable-contains-a-numerical-value-in-javascript and another http://stackoverflow.com/questions/1272696/checking-if-number-entered-is-a-digit-in-jquery – Dan Sep 11 '14 at 13:39
  • the case for the function isNumeric() are : http://api.jquery.com/jquery.isnumeric/ but i am asking how can i test if it is integer or not – Bouraoui KACEM Sep 11 '14 at 13:42
  • @Dan Easier to just ask than to try searching for a question you may not know how to phrase! – Mardoxx Sep 11 '14 at 13:42
  • Using isNAN? isNaN("Hello") => true – prakashapkota Sep 11 '14 at 13:44
  • @Mardoxx yeah but he does know how to phrase it - Can just google "Test if string is Integer with Jquery". – Dan Sep 11 '14 at 13:45

3 Answers3

9

thank you for your contributions i found the answer :

if(Math.floor($('#distance').val()) == $('#distance').val() && $.isNumeric($('#distance').val()))
 { 
 alert('yes its an int!');
 } 
Bouraoui KACEM
  • 821
  • 1
  • 8
  • 20
-1

I use this

function IsInteger(n){
    return Number(n) === n && n % 1 === 0;
}
Blake
  • 17
  • 2
  • A code block alone does not provide a good answer. Please add explanations (why it solve the issue, where was the mistake, etc...) – Louis Barranqueiro Jan 12 '16 at 20:45
  • OK Number(n) returns NaN, so if 'not a number' it won't equal n. The n % 1 will return 0 if an integer. Something else mean you have a float – Blake Jan 12 '16 at 21:32
  • IsInteger("555445")===> result =false – Bouraoui KACEM Jan 13 '16 at 10:18
  • If function is updated to the following, it would work for passing in string input. function IsInteger(n){ return Number(n) == n && n % 1 === 0; } Using "==" instead of "===" so "555445" == 5554455 will result in true. – J Adam Rogers Aug 04 '16 at 17:34
-4

sorry I didn't notice integer when I answered, here is another type check:

if($.type($(#distance).val())==="number")

please see: http://api.jquery.com/jquery.type/

Techy
  • 2,026
  • 4
  • 20
  • 41