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

8

3 Answers 3

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!');
 } 
Sign up to request clarification or add additional context in comments.

2 Comments

how it could be possible if it returns the alert for the input 876ffff654?? This is not true.
@LeaveCover give us the test link please
-1

I use this

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

4 Comments

A code block alone does not provide a good answer. Please add explanations (why it solve the issue, where was the mistake, etc...)
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
IsInteger("555445")===> result =false
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.
-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/

1 Comment

it returns "string" and not "number" because the input element has the attribute : type=text

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.