Use PHP functions in JavaScript

JavaScript is_numeric

Returns true if value is a number or a numeric string

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
function is_numeric (mixed_var) {
    // Returns true if value is a number or a numeric string  
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/is_numeric    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: David
    // +   improved by: taith
    // +   bugfixed by: Tim de Koning
    // +   bugfixed by: WebDevHobo (http://webdevhobo.blogspot.com/)    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: is_numeric(186.31);
    // *     returns 1: true
    // *     example 2: is_numeric('Kevin van Zonneveld');
    // *     returns 2: false    // *     example 3: is_numeric('+186.31e2');
    // *     returns 3: true
    // *     example 4: is_numeric('');
    // *     returns 4: false
    // *     example 4: is_numeric([]);    // *     returns 4: false
    return (typeof(mixed_var) === 'number' || typeof(mixed_var) === 'string') && mixed_var !== '' && !isNaN(mixed_var);
}
external links: original PHP docs | raw js source

Examples

» Example 1

Running

1
is_numeric(186.31);

Should return

1
true

» Example 2

Running

1
is_numeric('Kevin van Zonneveld');

Should return

1
false

Dependencies

No dependencies, you can use this function standalone.

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.


Authors

Thanks to the following developers, you get to have is_numeric goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Kevin van Zonneveld
3 Apr '09 Permalink

q  @ Tim de Koning: Thank you for noticing. I had to fix it a bit differently in but the bottom line is your testcase works now. Thanks!

Gravatar
Tim de Koning
31 Mar '09 Permalink

q  Hi Kevin e.a.

is_numeric('') returns true in javascript, not in PHP... Shouldn't this be:

function is_numeric( mixed_var ) {
    return !isNaN(parseInt(mixed_var));
}

Gravatar
Kevin van Zonneveld
2 Feb '09 Permalink

q  @ taith: Check, fixed!

Gravatar
taith
2 Feb '09 Permalink

q  some browsers will interpret a number as a string depending on how its set... hence a number, can be defined as a string, making the function return false all the time...

this will automatically turn it into an integer in this case

[CODE="Javascript"]
function is_numeric(integer){
return (!isNaN(integer*1));
}
[/CODE]

Gravatar
Kevin van Zonneveld
1 Mar '08 Permalink

q  @ Martijn Wieringa: And additional compliments for solid code. The integration went seamlessly, nice job!

Gravatar
Kevin van Zonneveld
1 Mar '08 Permalink

q  @ Martijn Wieringa: I will add the functions that are missing here, thanks alot man!

Gravatar
Martijn Wieringa
1 Mar '08 Permalink

q  I've been working on a simular project.. Here are some functions i completes so far.

[code="javascript"]

// Load PHP library
var PHP = new PHP_LIBRARY();

// Call some function within PHP library
PHP.func(params);

[/code]

Here's my library (so far)

[code="javascript"]

var PHP_LIBRARY = function() {}

PHP_LIBRARY.prototype =
{
'abs' : function(f_float)
{
return isNaN(f_float) ? 0 : Math.abs(f_float);
},

'chr' : function(f_ascii)
{
return String.fromCharCode(f_ascii);
},

'explode' : function(f_seperator, f_string)
{
return f_string.split(f_seperator);
},

'implode' : function(f_glue, f_array)
{
return f_array.join(f_glue);
},

'join' : function(f_glue, f_array)
{
return this.implode(f_glue, f_array);
},

'number_format' : function(f_float, f_decimals, f_decimal_sign, f_thousand_sign)
{
if(f_decimals == undefined)
{
f_decimals = 0;
}

if(f_decimal_sign == undefined)
{
f_decimal_sign = '';
}

if(f_thousand_sign == undefined)
{
f_thousand_sign = '';
}

var result = this.implode(f_thousand_sign, this.str_split(Math.floor(f_float).toString(), 3, true));

if(f_decimals > 0)
{
var d = Math.round((f_float % 1) * Math.pow(10, f_decimals)).toString();
result += f_decimal_sign + d + this.str_repeat('0', f_decimals - d.length);
}

return result;
},

'ord' : function(f_string)
{
return f_string.charCodeAt(0);
},

'split' : function(f_seperator, f_string)
{
return this.explode(f_seperator, f_string);
},

'str_repeat' : function(f_string, f_repeat)
{
var result = '';

while(f_repeat > 0)
{
result += f_string;
f_repeat--;
}

return result;
},

'str_replace' : function(f_needle, f_replace, f_haystack)
{
var result = '';
var index = 0;

while((index = f_haystack.indexOf(f_needle)) > -1)
{
result += f_haystack.substring(0, index);
result += f_replace;
f_haystack = f_haystack.substring(index + f_needle.length);
}

return result + f_haystack;
},

'str_ireplace' : function(f_needle, f_replace, f_haystack)
{
var result = '';
var index = 0;

var haystack = f_haystack.toLowerCase();
var needle = f_needle.toLowerCase();

while((index = haystack.indexOf(needle)) > -1)
{
result += f_haystack.substring(0, index);
result += f_replace;

haystack = haystack.substring(index + f_needle.length);
f_haystack = f_haystack.substring(index + f_needle.length);
}

return result + f_haystack;
},

'str_split' : function(f_string, f_split_length, f_backwards)
{
if(f_backwards == undefined)
{
f_backwards = false;
}

if(f_split_length > 0)
{
var result = new Array();

if(f_backwards)
{
var r = (f_string.length % f_split_length);

if(r > 0)
{
result[result.length] = f_string.substring(0, r);
f_string = f_string.substring(r);
}
}

while(f_string.length > f_split_length)
{
result[result.length] = f_string.substring(0, f_split_length);
f_string = f_string.substring(f_split_length);
}

result[result.length] = f_string;

return result;
}

return false;
},

'strcasecmp' : function(f_string1, f_string2)
{
var string1 = f_string1.toLowerCase();
var string2 = f_string2.toLowerCase();

if(string1 > string2)
{
return 1;
}
else if(string1 == string2)
{
return 0;
}

return -1;
},

'strcmp' : function(f_string1, f_string2)
{
if(f_string1 > f_string2)
{
return 1;
}
else if(f_string1 == f_string2)
{
return 0;
}

return -1;
},

'stripos' : function(f_haystack, f_needle, f_offset)
{
var haystack = f_haystack.toLowerCase();
var needle = f_needle.toLowerCase();
var index = 0;

if(f_offset == undefined)
{
f_offset = 0;
}

if((index = haystack.indexOf(needle, f_offset)) > -1)
{
return index;
}

return false;
},

'strlen' : function(f_string)
{
return f_string.length;
},

'strnatcasecmp' : function(f_string1, f_string2, f_version)
{
this.strnatcmp(f_string1.toLowerCase(), f_string2.toLowerCase(), f_version);
},

'strnatcmp' : function(f_string1, f_string2)
{
if(f_version == undefined)
{
f_version = false;
}

var array1 = this.__strnatcmp_split(f_string1);
var array2 = this.__strnatcmp_split(f_string2);

var len = array1.length;
var text = true;

var result = -1;
var r = 0;

if(len > array2.length)
{
len = array2.length;
result = 1;
}

for(i = 0; i < len; i++)
{
if(isNaN(array1[i]))
{
if(isNaN(array2[i]))
{
text = true;

if((r = this.strcmp(array1[i], array2[i])) != 0)
{
return r;
}
}
else if(text)
{
return 1;
}
else
{
return -1;
}
}
else if(isNaN(array2[i]))
{
if(text)
{
return -1;
}
else
{
return 1;
}
}
else
{
if(text || f_version)
{
if((r = (array1[i] - array2[i])) != 0)
{
return r;
}
}
else
{
if((r = this.strcmp(array1[i].toString(), array2[i].toString())) != 0)
{
return r;
}
}

text = false;
}
}

return result;
},

'__strnatcmp_split' : function(f_string)
{
var result = new Array();
var buffer = '';
var chr = '';

var text = true;

for(var i = 0; i < f_string.length; i++)
{
chr = f_string.substring(i, i + 1);

if(chr.match(/[0-9]/))
{
if(text)
{
if(buffer.length > 0)
{
result[result.length] = buffer;
buffer = '';
}

text = false;
}

buffer += chr;
}
else if((text == false) && (chr == '.') && (i < (f_string.length - 1)) && (f_string.substring(i + 1, i + 2).match(/[0-9]/)))
{
result[result.length] = buffer;
buffer = '';
}
else
{
if(text == false)
{
if(buffer.length > 0)
{
result[result.length] = parseInt(buffer);
buffer = '';
}

text = true;
}

buffer += chr;
}
}

if(buffer.length > 0)
{
if(text)
{
result[result.length] = buffer;
}
else
{
result[result.length] = parseInt(buffer);
}
}

return result;
},


'strpos' : function(f_haystack, f_needle, f_offset)
{
var index = 0;

if(f_offset == undefined)
{
f_offset = 0;
}

if((index = f_haystack.indexOf(f_needle, f_offset)) > -1)
{
return index;
}

return false;
},

'strrev' : function(f_string)
{
var result = '';
var index = f_string.length - 1;

while(index >= 0)
{
result += f_string.substring(index, index + 1);
index--;
}

return result;
},

'strripos' : function(f_haystack, f_needle, f_offset)
{
var haystack = f_haystack.toLowerCase();
var needle = f_needle.toLowerCase();
var index = 0;

if((index = haystack.indexOf(needle, f_offset)) > -1)
{
do
{
f_offset = index;
}
while((index = haystack.indexOf(needle, f_offset + 1)) > -1);

return f_offset;
}

return false;
},

'strrpos' : function(f_haystack, f_needle, f_offset)
{
var index = 0;

if((index = f_haystack.indexOf(f_needle, f_offset)) > -1)
{
do
{
f_offset = index;
}
while((index = f_haystack.indexOf(f_needle, f_offset + 1)) > -1);

return f_offset;
}

return false;
},

'strtolower' : function(f_string)
{
return f_string.toLowerCase();
},

'strtoupper' : function(f_string)
{
return f_string.toUpperCase();
},

'substr' : function(f_string, f_start, f_length)
{
if(f_start < 0)
{
f_start += f_string.length;
}

if(f_length == undefined)
{
f_length = f_string.length;
}
else if(f_length < 0)
{
f_length += f_string.length;
}
else
{
f_length += f_start;
}

if(f_length < f_start)
{
f_length = f_start;
}

return f_string.substring(f_start, f_length);
},

'substr_count' : function(f_haystack, f_needle, f_offset)
{
var result = 0;
var index = 0;

if(f_offset == undefined)
{
f_offset = 0;
}

while((index = f_haystack.indexOf(f_needle, f_offset + 1)) > -1)
{
result++;
f_offset = index;
}

return result;
},

'trim' : function(f_string)
{
return f_string.replace(/^\s*/, '').replace(/\s*$/, '');
},

'ucfirst' : function(f_string)
{
return f_string.substring(0, 1).toUpperCase() + f_string.substring(1);
},

'ucword' : function(f_string)
{
var result = '';
var chr = '';
var swap = true;

for(var i = 0; i < f_string.length; i++)
{
chr = f_string.substring(i, i + 1);

if(swap)
{
result += chr.toUpperCase();
}
else
{
result += chr;
}

if(chr.match(/\s/))
{
swap = true;
}
else
{
swap = false;
}
}

return result;
}
}


[/code]

Gravatar
Kevin van Zonneveld
16 Jan '08 Permalink

q  @ David: Thank you for noticing. I thouhgt of isNaN(), which I think does the trick. If you wan't to be credited differently let me know.

Gravatar
David
16 Jan '08 Permalink

q  One of your tests should be:

is_numeric("+186.31e2");

And that needs to return true.

Gravatar
David
16 Jan '08 Permalink

q  The is_numeric function is not correct, at least it doesn't work like PHP. A numeric string, like "-876.20" should return true, but it doesn't because it doesn't pass the [typeof mixed_var == 'number'] condition.


Contribute a New function

More functions

In this category

doubleval
empty
floatval
get_defined_vars
get_resource_type
gettype
import_request_variables
intval
is_array
is_binary
is_bool
is_buffer
is_callable
is_double
is_float
is_int
is_integer
is_long
is_null
» is_numeric
is_object
is_real
is_resource
is_scalar
is_string
is_unicode
isset
print_r
serialize
settype
strval
unserialize
var_dump
var_export

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: @HalfWinter, Paulo Freitas, Andros Peña Romo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !