Page 1 of 1

Numbers and commas only

Posted: Wed Aug 03, 2005 12:11 am
by Mr Tech
I have a script that I want to use if() to check adn see if the $string has only numbers and commas.

E.g:

if (isnumber($number)) {
echo "it is a number";
} else {
echo "it is not jsut number";
}

Posted: Wed Aug 03, 2005 12:13 am
by feyd
are you asking, or offerring to share, or what?

Posted: Wed Aug 03, 2005 12:21 am
by Mr Tech
Meh... sorry, I forgot to ad that. I'm asking :roll:

Sorry I was vague.

Posted: Wed Aug 03, 2005 12:25 am
by feyd
since you didn't specify specifics as to format...

Code: Select all

^[0-9,]+$

Posted: Wed Aug 03, 2005 12:36 am
by Mr Tech
Thanks feyd.

I put it into this:

Code: Select all

<?php
$what = "2,d67";
if (preg_match("/^[0-9,]+$/is", $what, $match )) {
echo "yep";
} else {
echo "nope";
}
?>
Is that the best way to do it?

Posted: Wed Aug 03, 2005 3:08 am
by timvw
I usually do it like:

Code: Select all

if ($what == strval(floatval($what))
{
  //
}