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
The is_numeric() function checks whether a variable is a number or a numeric string. This function returns true (1) if the variable is a number or a numeric string, otherwise it returns false/nothing.
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"
}
what about
if (preg_match('/^[0-9]+$/', $str)) {
echo "valid";
} else {
echo "invalid";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With