|
-
Sep 26th, 2003, 04:06 AM
#1
Thread Starter
Lively Member
Check for numbers in variable RESOLVED
What's the best way of checking that a variable only has numbers in it, so no letters or commas etc.
Last edited by aaronskw; Sep 29th, 2003 at 06:41 AM.
-
Sep 26th, 2003, 04:51 PM
#2
Member
Use the is_numeric function.
PHP Code:
<?php
$var1= "12three"; // alphanumeric
$var2 = "123"; // numeric string
$var3 = 123; // integer
if(is_numeric($var1))
{
echo "this will not be echo'd because it is false";
}
if(is_numeric($var2))
{
echo "this will be echo'd because it is true";
}
if(is_numeric($var3))
{
echo "this will be echo'd because it is true";
}
?>
If its from an imput form or something that's going to be a little but "less then perfect", you might want to do some cleanup to remove trailing spaces etc. (trim)
-
Sep 29th, 2003, 04:02 AM
#3
Thread Starter
Lively Member
thnx for the reply, i'll give it a go.
-
Sep 29th, 2003, 06:40 AM
#4
Thread Starter
Lively Member
It seems there's no need to do a Trim for an input form, bonus!!!
Thnx again.
-
Sep 30th, 2003, 10:09 AM
#5
Frenzied Member
careful as is_numeric will also detect this as numeric
4e4 and 444 // those are both correct.
you want this
Code:
if (preg_match ("/^([0-9]+)$/", $input_number)) {
echo "true";
} else {
echo "false";
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|