11

I want to detect if a string I have contain only number, not containing a letter, comma, or dot. For example like this:

083322 -> valid
55403.22 -> invalid
1212133 -> valid
61,23311 -> invalid
890022 -> valid
09e22 -> invalid

I already used is_numeric and ctype_digit but it's not valid

simple guy
  • 565
  • 1
  • 5
  • 15

2 Answers2

22

You want to use preg_match in that case as both 61,23311 and 55403.22 are valid numbers (depending on locale). i.e.

if (preg_match("/^\d+$/", $number)) {
    return "is valid"
} else {
    return "invalid"
}
Kasia Gogolek
  • 3,269
  • 4
  • 30
  • 48
5

what about

if (preg_match('/^[0-9]+$/', $str)) {
  echo "valid";
} else {
  echo "invalid";
}