Forum Moderators: coopster

Message Too Old, No Replies

ereg help

         

ikke

11:59 am on Mar 30, 2005 (gmt 0)

10+ Year Member



Hello,
I have the following code:

if (!empty($aantal))
{
$theresults = ereg("[0-9]", $aantal, $trashed);
if ($theresults) {$isamatch = "Yes"; } else { $isamatch = "No"; }
if ($isamatch == "No")
{
echo "<SCRIPT>alert(\"ERROR: Geen correct aantal\");</SCRIPT>";
$aantal = "0";
}
}

When I run this it only filters the first character so when the value = 1.3 he just ignore it. but when it`s .1 it gives the error.

Does anyone have an idea.

Thanks

coopster

12:02 pm on Mar 30, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You have to tell it to look at more than one character.
$theresults = ereg("[0-9]+", $aantal, $trashed);

ikke

12:23 pm on Mar 30, 2005 (gmt 0)

10+ Year Member



This is what i get:

$theresults = ereg("[0-9]+[0-9]+[0-9]", $aantal, $trashed);

But that needs a input of 3 character and it is dynamic input so sometimes 1 or 2 characters. I tried it with? but that didn`t work

dreamcatcher

1:00 pm on Mar 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thats not what coopster meant ikke. The + operator in regex means one or more of the preceding items. You only need use it once

[0-9]+

If you are trying to evaluate if the string is a digit, use the character class [[:digit:]]

$theresults = ereg("^[[:digit:]]+", $aantal);

dc

ikke

1:23 pm on Mar 30, 2005 (gmt 0)

10+ Year Member



I had that but it still doesn`t give an error only when it is the first character. so 1....555.. is stil a valid input with the following code:

$theresults = ereg("^[[:digit:]]+", $aantal, $trashed);

So now I`m lost

ikke

9:20 am on Mar 31, 2005 (gmt 0)

10+ Year Member



Thanks for the help.

The following code is working:


if (eregi('[^0-9]', $aantal))
{
echo "<SCRIPT>alert(\"ERROR: Geen correct aantal\");</SCRIPT>";
$aantal = "0";
}