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\'
You should use: $.isNumeric( i )
jQuery.isNumeric API
Be aware that empty string ''
and ' '
will be considered as number by isNaN
and isFinite
.
See isNan
EDIT: Please look at what Christoph has suggested above. It makes sense to use isFinite as suggested.
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
The javascript function isNaN(variable)
should do the trick. It returns true if the data is not a number.
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.