Richard Knop
Richard Knop

Reputation: 83677

Check that variable is a number

How can I check that a variable is a number, either an integer or a string digit?

In PHP I could do:

if (is_int($var)) {
    echo '$var is integer';
}

Or:

if (is_numeric($var)) {
    echo '$var is numeric';
}

How can I do it in jQuery/JavaScript?

Upvotes: 39

Views: 77467

Answers (10)

MD SHAYON
MD SHAYON

Reputation: 8055

The typeof operator returns a string indicating the type of the unevaluated operand.

const num = 42;
console.log(typeof num === "number"); // expected return true
if(typeof num === "number"){
   console.log("This is number")
}

More on it......

console.log(typeof 42);
// expected output: "number"

console.log(typeof 'blubber');
// expected output: "string"

console.log(typeof true);
// expected output: "boolean"

console.log(typeof undeclaredVariable);
// expected output: "undefined"

Upvotes: -1

Mechisso
Mechisso

Reputation: 61

You should use:

if(Number.isInteger(variable))
   alert("It is an integer");
else
   alert("It is not a integer");

you can find the reference in: Number.isInteger()

Upvotes: 6

FenixAdar
FenixAdar

Reputation: 47

You should use: $.isNumeric( i )

jQuery.isNumeric API

Upvotes: 0

AnomalySmith
AnomalySmith

Reputation: 637

Be aware that empty string '' and ' ' will be considered as number by isNaN and isFinite.

Upvotes: 0

Skull
Skull

Reputation: 1252

You should use

simple way to check whether given value is number by using "if condition"

function isInteger(value)      
{       
    num=value.trim();         
    return !(value.match(/\s/g)||num==""||isNaN(num)||(typeof(value)=='number');        
}

it will return true if the value which we are passing is an integer..

solved for     
value=""      //false null     
value="12"    //true only integers       
value="a"     //false     
value=" 12"   //false      
value="12 "   //false         
value=" "     //false space        
value="$12"   //false special characters 
value="as12"    //false

Upvotes: 0

Kuldeep Singh
Kuldeep Singh

Reputation: 309

You should use:

var x = 23;
var y = 34hhj;

isNaN(x); 
isNaN(y); 

It will return true if its string and false if its a number.

Upvotes: 1

JonnyRaa
JonnyRaa

Reputation: 8038

I'm pretty new to javascript but it seems like typeof(blah) lets you check if something is a number (it doesn't say true for strings). A know the OP asked for strings + numbers but I thought this might be worth documenting for other people.

eg:

function isNumeric(something){
    return typeof(something) === 'number';
}

Here are the docs

and here's a few console runs of what typeof produces:

typeof(12);
"number"
typeof(null);
"object"
typeof('12');
"string"
typeof(12.3225);
"number"  

one slight weirdness I am aware of is

typeof(NaN);
"number"

but it wouldn't be javascript without something like that right?!

Upvotes: 13

Christoph
Christoph

Reputation: 169523

I'd go with

isFinite(String(foo))

See this answer for an explanation why. If you only want to accept integer values, look here.

Upvotes: 27

Decent Dabbler
Decent Dabbler

Reputation: 22773

function isNumeric( $probe )
{
    return parseFloat( String( $probe ) ) == $probe;
}

Upvotes: 1

AAA
AAA

Reputation: 4956

The javascript function isNaN(variable) should do the trick. It returns true if the data is not a number.

Upvotes: 45

Related Questions