JSFiddle - React, Tailwind, and code Playground
by Brad Christie
CSS
b{
display: inline-block;
width: 150px;
}
JavaScript
var tests = ['apple',
'4',
'5.6',
'v8',
'hello, world!',
'7even',
'-156',
'-5216.056',
'656-5651',
'-6'];
function IsNumeric(n){
// any valid number
//return /^-?\d+(?:\.\d+)?$/.test(n);
// only positive numbers
//return /^\d+(?:\.\d+)?$/.test(n);
// only positive whole numbers
return /^\d+$/.test(n);
}
$.each(tests,function(i,t){
var s = $('<span>');
$('<p>').html('<b>'+t+'</b> ')
.append(IsNumeric(t)?s.text('pass').css('color','green'):s.text('fail').css('color','red'))
.appendTo('body');
});