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: 911.718
    // 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:

1
2
3
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

1
2
3
function is_numeric(integer){
 return (!isNaN(integer*1));
}

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.

1
2
3
4
5
// Load PHP library
var PHP = new PHP_LIBRARY();
 
// Call some function within PHP library
PHP.func(params);


Here's my library (so far)

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
39
4041
42
43
44
4546
47
48
49
5051
52
53
54
5556
57
58
59
6061
62
63
64
6566
67
68
69
7071
72
73
74
7576
77
78
79
8081
82
83
84
8586
87
88
89
9091
92
93
94
9596
97
98
99
100101
102
103
104
105106
107
108
109
110111
112
113
114
115116
117
118
119
120121
122
123
124
125126
127
128
129
130131
132
133
134
135136
137
138
139
140141
142
143
144
145146
147
148
149
150151
152
153
154
155156
157
158
159
160161
162
163
164
165166
167
168
169
170171
172
173
174
175176
177
178
179
180181
182
183
184
185186
187
188
189
190191
192
193
194
195196
197
198
199
200201
202
203
204
205206
207
208
209
210211
212
213
214
215216
217
218
219
220221
222
223
224
225226
227
228
229
230231
232
233
234
235236
237
238
239
240241
242
243
244
245246
247
248
249
250251
252
253
254
255256
257
258
259
260261
262
263
264
265266
267
268
269
270271
272
273
274
275276
277
278
279
280281
282
283
284
285286
287
288
289
290291
292
293
294
295296
297
298
299
300301
302
303
304
305306
307
308
309
310311
312
313
314
315316
317
318
319
320321
322
323
324
325326
327
328
329
330331
332
333
334
335336
337
338
339
340341
342
343
344
345346
347
348
349
350351
352
353
354
355356
357
358
359
360361
362
363
364
365366
367
368
369
370371
372
373
374
375376
377
378
379
380381
382
383
384
385386
387
388
389
390391
392
393
394
395396
397
398
399
400401
402
403
404
405406
407
408
409
410411
412
413
414
415416
417
418
419
420421
422
423
424
425426
427
428
429
430431
432
433
434
435436
437
438
439
440441
442
443
444
445446
447
448
449
450451
452
453
454
455456
457
458
459
460461
462
463
464
465466
467
468
469
470471
472
473
474
475476
477
478
479
480481
482
483
484
485486
487
488
489
490491
492
493
494
495496
497
498
499
500501
502
503
504
505506
507
508
509
510511
512
513
514
515516
517
518
519
520521
522
523
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;
        }
}

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