Page 1 of 1

float regular expression

Posted: Wed Mar 02, 2005 11:02 am
by dannymc1983
anyone know what the regular expression is for a float number that only allows values of 0.0 to 4.0?

Posted: Wed Mar 02, 2005 11:05 am
by n00b Saibot
I'm not a regex expert but a try

Code: Select all

/ї0-4]\.0/

Posted: Wed Mar 02, 2005 11:19 am
by dannymc1983
nope, didnt work. i tried it with this code:

Code: Select all

$val = 2.8;

if (!preg_match('/ї0-4]\.0/', $val))
{
echo "<script language = 'javascript'>alert('"$val" is not a                     proper float value')</script>";
exit;
&#125;
any other suggestions?

Posted: Wed Mar 02, 2005 11:31 am
by feyd

Code: Select all

#(&#1111;0-3](\.&#1111;0-9]+)?|4(\.0+)?)#
:)

Posted: Wed Mar 02, 2005 11:31 am
by n00b Saibot
oops forgot something. that pattern will match only 1.0,2.0,3.0 and 4.0
try this one

Code: Select all

/&#1111;0-4]\.&#1111;0-9]/

Posted: Wed Mar 02, 2005 1:23 pm
by dannymc1983
feyd, thanks for that but i dont think its working. you'll see if you try running this code:

Code: Select all

<?
$val = -1;

		if (!preg_match('#(&#1111;0-3](\.&#1111;0-9]+)?|4(\.0+)?)#', $val))
		&#123;
			echo "$val is not a proper float value";
		&#125;
		else
			echo "$val is a proper float value";
?>
what i need is just a regex that only accepts values between 0.00 and 4.00 inclusive (ie. 0.00 and 4.00 are allowed values). so it will not accept values such as -1 or 4.1 but will accept 3.22 or 1.97
thanks


feyd | :roll:

Posted: Wed Mar 02, 2005 2:16 pm
by smpdawg
How about this?

Code: Select all

#^(&#1111;0-3](\.&#1111;0-9]+)?|4(\.0+)?)#
Please fix your code tags in your previous post as it helps make it more readable.

Thanks

Posted: Wed Mar 02, 2005 3:22 pm
by feyd
why would you use a regular expression to check an actual number?

Code: Select all

if($val >= 0 && $val <= 4)
works FAR faster..:?

Posted: Wed Mar 02, 2005 6:40 pm
by dannymc1983
that true i suppose!
thanks for that and what exactly was wrong with my code tags so i know for future reference to my code???

Posted: Wed Mar 02, 2005 6:43 pm
by smpdawg
For some reason the first code tag was missing. It isn't now so I suspect a moderator may have done it for you.

I usually preview before I post because the tags get screwed up sometimes even when I am sure I hit that button.

Posted: Wed Mar 02, 2005 9:54 pm
by feyd
the code tags were reversed.. a la





Posted: Thu Mar 03, 2005 12:30 am
by n00b Saibot
i think me and regexes had a little fight in pre-historic era. they never come to me rightly :( :lol: