Bouraoui KACEM
Bouraoui KACEM

Reputation: 821

Test if string is Integer with Jquery

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

Upvotes: 6

Views: 30023

Answers (3)

Blake
Blake

Reputation: 17

I use this

function IsInteger(n){
    return Number(n) === n && n % 1 === 0;
}

Upvotes: -1

Techy
Techy

Reputation: 2132

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/

Upvotes: -4

Bouraoui KACEM
Bouraoui KACEM

Reputation: 821

thank you for your contributions i found the answer :

if(Math.floor($('#distance').val()) == $('#distance').val() && $.isNumeric($('#distance').val()))
 { 
 alert('yes its an int!');
 } 

Upvotes: 9

Related Questions