Click to See Complete Forum and Search --> : isnumeric


afsarsal
08-04-2003, 06:28 AM
Hi,
Is there a method like isnumeric to control a text value?
Thanks in advance,
Salim

Charles
08-04-2003, 06:33 AM
<script type="text/javascript">
<!--
String.prototype.isNumeric = function () {return !isNaN(this)}

alert ('foo'.isNumeric());
alert (Math.PI.toString().isNumeric());
// -->
</script>

Gollum
08-04-2003, 06:34 AM
you could call parseInt and test if the number is a number
e.g.

var s = "12345";
if ( !parseInt(s).isNaN() ) alert("'" + s "' is a number");

Charles
08-04-2003, 06:44 AM
Originally posted by Gollum
you could call parseInt and test if the number is a number
e.g.

var s = "12345";
if ( !parseInt(s).isNaN() ) alert("'" + s "' is a number");
I suppose you could, but you would find that it doesn't work.

kyle969
08-04-2003, 08:47 AM
function isDigit(c){
return((c >= "0") && (c <= "9"))
}

function isInteger(s){
var c, i;
for(i = 0; i < 5; i++)
if(!isDigit(s.charAt(i))) return false;
return true;
}

Gollum
08-04-2003, 08:58 AM
Ahh, thankyou Charles for spotting my mistake. I guess I deserve it for not checking the code before I posted.

I like your solution though. Neat and concise.