regular expresion
| From: | Richard | Date: | Fri, 19 Oct 2001 23:06:29 +0000 |
| Subject: | regular expresion | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-71647@lists.php.net to get a copy of this message | ||
Hi, I want to validate that a variable only contains numbers. I came up with
this code
$variable = "154545"; -> must give me true.
$variable = "$%54545"; -> must give me false
$variable = "$% 54545"; -> must give me false
$variable = "4545;#"; -> must give me false
$variable = "004545"; -> must give me false
CODE STARTS HERE
$variable = "55555";
$variable = trim($variable);
if (ereg ("[a-zA-Z]", $variable)) {
echo "NOT ONLY NUMBERS: $variable";
$variable = "";
}
if (ereg ("[0-9]", $variable)) {
$variable = $variable * 1;
if ($variable == 0) {
echo "IS NOT A NUMBER";
} else {
echo "JUST NUMBERS: $variable";
}
}
Another thing that I thought about was this :
$variable= ereg_replace( "[^0-9]", "", $variable );
But is not what i am looking for...
I know there is a simpler way to do it. I just couldn't figure it out.
Thanks in advance....
Ricardo