note 74001 added to function.is-numeric
| From: | www dot kigoobe dot com at osu1 dot php dot net | Date: | Tue, 20 Mar 2007 13:55:55 +0000 |
| Subject: | note 74001 added to function.is-numeric | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-124625@lists.php.net to get a copy of this message | ||
If you are looking for a way to check if the user input is a number, either positive or negative, or
a decimal value (but not with an e, as in is_numeric), the following can be used -
<?php
mynumber = "3" // 3 is a number
$mynumber = "3.3" // 3.3 is a number
$mynumber = "-3.3" // -3.3 is a number
$mynumber = "-0,3" // -0,3 is a number
$mynumber = "-0,3x" // -0,3x is not a number
if (preg_match ("/^([0-9.,-]+)$/", $mynumber)) {
print $mynumber." is a number";
} else {
print $mynumber." is not a number";
}
?>
I have added comma (,) as in many european number system, decimal is expressed with a comma, and not
with a dot. Like, $100 is expressed as $100,00 instead of $100.00
if you don't need that, remove the (,) and, you can add any additional value that you need to
add after the series 0-9.,-
----
Server IP: 217.174.196.197
Probable Submitter: 86.69.194.197
----
Manual Page -- http://www.php.net/manual/en/function.is-numeric.php
Edit -- https://master.php.net/note/edit/74001
Del: integrated -- https://master.php.net/note/delete/74001/integrated
Del: useless -- https://master.php.net/note/delete/74001/useless
Del: bad code -- https://master.php.net/note/delete/74001/bad+code
Del: spam -- https://master.php.net/note/delete/74001/spam
Del: non-english -- https://master.php.net/note/delete/74001/non-english
Del: in docs -- https://master.php.net/note/delete/74001/in+docs
Del: other reasons-- https://master.php.net/note/delete/74001
Reject -- https://master.php.net/note/reject/74001
Search -- https://master.php.net/manage/user-notes.php