float regular expression

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dannymc1983
Forum Commoner
Posts: 80
Joined: Wed Feb 16, 2005 7:24 am

float regular expression

Post by dannymc1983 »

anyone know what the regular expression is for a float number that only allows values of 0.0 to 4.0?
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

I'm not a regex expert but a try

Code: Select all

/ї0-4]\.0/
dannymc1983
Forum Commoner
Posts: 80
Joined: Wed Feb 16, 2005 7:24 am

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

#(&#1111;0-3](\.&#1111;0-9]+)?|4(\.0+)?)#
:)
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post 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]/
dannymc1983
Forum Commoner
Posts: 80
Joined: Wed Feb 16, 2005 7:24 am

Post 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:
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..:?
dannymc1983
Forum Commoner
Posts: 80
Joined: Wed Feb 16, 2005 7:24 am

Post 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???
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the code tags were reversed.. a la




User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

i think me and regexes had a little fight in pre-historic era. they never come to me rightly :( :lol:
Post Reply