Jump to content

[SOLVED] preg_match not working properly???


darkfreaks

Recommended Posts

okay so when i enter a number i get "only numbers allowed"

 

its sposed to not match 0-9 and then echo "numbers are only allowed"

 

so im baffled where i went wrong ???

 

<?php
$height= trim($_POST['height']);
$weight=trim($_POST['weight']);

@$total=$weight/$height*2;
$newtotal= $total*703;
$output = sprintf('%01.2f',$newtotal);
if($_POST['weight']==""||empty($_POST['weight'])){$error['weight']="<font color=red>Please enter a weight!</font>";} else {unset($error['weight']);}
if($_POST['height']==""||empty($_POST['height'])){$error['height']="<font color=red>Please enter a height!</font>";} else {unset($error['height']);} 
if(!preg_match("[0-9]",$_POST['height'])){$error['height']="<font color=red>Only Numbers Allowed!</font>";}else{unset($error['height']);} 
if(!preg_match("[0-9]",$_POST['weight'])){$error['weight']="<font color=red>Only Numbers Allowed!</font>";}else{unset($error['weight']);} 
if($error) {?>

<form method="POST">

<b>Height(inches):</b><input name="height" type="text" size="3"><?php if ($_POST) { echo $error['height'];} ?><b>Weight(lbs.):</b><input name="weight" type="text" size="3"><?php if ($_POST) { echo $error['weight'];} ?>
<input type="submit" name="Calculate!" value="Calculate!">

</form>
<?php } else{
switch($newtotal)
{
    case ($newtotal < 18):
    $msg = "<font color=red>You're Underweight! Youre BMI is:$output%</font>";
    break;

    case ($newtotal >= 18 && $newtotal < 24.9):
    $msg = "<font color=green>You're Healthy! Youre BMI is:$output%</font>";
    break;

    case ($newtotal >= 24.9 && $newtotal < 29.9):
    $msg = "<font color=orange>You're Overweight! Youre BMI is:$output%</font>";
    break;

    default:
    $msg = "<font color=red>You Are Obese Please seek Immediate Medical Help!</font>";
    break;
}
echo $msg;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/153977-solved-preg_match-not-working-properly/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.