Page 1 of 1
SUPER QUICK HELP needed for short regex!
Posted: Fri Jan 06, 2006 1:05 pm
by seodevhead
I just want to make sure the user types in a regular number... but this RegEx is allowing:
16..2 (two decimals)
How can I correct this to allow only 1 decimal? I thought that is what the ? is for?? Thanks!
Posted: Fri Jan 06, 2006 2:22 pm
by bundyo
Umm,
as far as i can see it - ([0-9]){1,2} matches a digit one or two times, then (\.)? matches optional dot, then again digit one or two times...
However i didn't understand exactly what are you trying to match?
Posted: Fri Jan 06, 2006 3:30 pm
by seodevhead
I just want to know how I can ammend this RegEx that I already have to not allow 2 dots like I example in the first post. I dont understand why 2 dots are allowed.
Posted: Fri Jan 06, 2006 3:34 pm
by timvw
That is because if you use this with preg_match it will not work how you think...
It will return true as soon as it finds that matches the regular expression, even timvw1.2 would be valid.
Untested, but i think the following would be what you need:
^([0-9]){1,2}(\.)?([0-9]){1,2}$
Posted: Fri Jan 06, 2006 3:52 pm
by seodevhead
Actually I am using eregi() to test the regex. So was my flaw leaving out the ^ at the start and $ at the end? It just seems odd to me that 2 dots are being accepted like this.
Posted: Fri Jan 06, 2006 5:06 pm
by timvw
You are only testing if the regular expression is matched... So as soon as you had two numbers in the value, it would have been accepted... No matter where they were somewhere in the string..
With the ^ and $ you can enforce there is nothing "extra"

Posted: Fri Jan 06, 2006 7:19 pm
by seodevhead
Even using:
'^([0-9]){1,2}(\.)?([0-9]){0,2}$'
still allows 16..2 as input. (two dots).
Still can't understand why...
Posted: Fri Jan 06, 2006 7:40 pm
by timvw
If you use posix regular expressions, you should lookup the syntax yourself..
With preg_match it works as expected:
timvw@madoka:~$ cat test.php
Code: Select all
<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);
if (preg_match("#^([0-9]){1,2}(\.)?([0-9]){0,2}$#", "16..2")) {
echo "there is a match\n";
} else {
echo "no match\n";
}
?>
Code: Select all
timvw@madoka:~$ php test.php
no match
Posted: Fri Jan 06, 2006 7:56 pm
by seodevhead
Man... I must live in like a 3rd dimension or something.. cause I copied the exact code you pasted into my script and it is still accepting 16..2 (2 dots). I cannot understand why. It is NOT accepting 9..2 thought... which is correct.. but why is it accepting 2 numbers and 2 dots is unreal, because I thought the ? is what says that the dot must either not come or only come once. I really appreciate your help tim.. especially taking the time to test in on your system. Unfortunatly the regex gods don't like my server.

Not sure what to do.
Posted: Sat Jan 07, 2006 2:37 am
by Jenk
Code: Select all
<?php
if (strval(floatval($_POST['num'])) === $_POST['num']) {
//number is a float..
} else {
//number is not a float..
}
?>
Posted: Sat Jan 07, 2006 6:28 am
by twigletmac
There is always
is_numeric()
Mac
Posted: Sat Jan 07, 2006 1:26 pm
by seodevhead
You have to remember that is_numeric() aint foolproof because it allows things like:
"20"
'20'
4e20
etc
All are numeric because they represent numbers. Not to mention 1....4 is a number as well.
Posted: Sat Jan 07, 2006 6:16 pm
by feyd
all three examples you just gave are numbers.

Posted: Mon Jan 09, 2006 9:59 am
by pickle