float regular expression
Moderator: General Moderators
-
dannymc1983
- Forum Commoner
- Posts: 80
- Joined: Wed Feb 16, 2005 7:24 am
float regular expression
anyone know what the regular expression is for a float number that only allows values of 0.0 to 4.0?
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
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
nope, didnt work. i tried it with this code:
any other suggestions?
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;
}- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
#(ї0-3](\.ї0-9]+)?|4(\.0+)?)#- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
oops forgot something. that pattern will match only 1.0,2.0,3.0 and 4.0
try this one
try this one
Code: Select all
/ї0-4]\.ї0-9]/-
dannymc1983
- Forum Commoner
- Posts: 80
- Joined: Wed Feb 16, 2005 7:24 am
feyd, thanks for that but i dont think its working. you'll see if you try running this code:
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 |
Code: Select all
<?
$val = -1;
if (!preg_match('#(ї0-3](\.ї0-9]+)?|4(\.0+)?)#', $val))
{
echo "$val is not a proper float value";
}
else
echo "$val is a proper float value";
?>thanks
feyd |
- smpdawg
- Forum Contributor
- Posts: 292
- Joined: Thu Jan 27, 2005 3:10 pm
- Location: Houston, TX
- Contact:
How about this?
Please fix your code tags in your previous post as it helps make it more readable.
Thanks
Code: Select all
#^(ї0-3](\.ї0-9]+)?|4(\.0+)?)#Thanks
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
why would you use a regular expression to check an actual number?
works FAR faster..
Code: Select all
if($val >= 0 && $val <= 4)-
dannymc1983
- Forum Commoner
- Posts: 80
- Joined: Wed Feb 16, 2005 7:24 am
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact: