PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a PHP Expert!

Join 307,035 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,282 people online right now. Registration is fast and FREE... Join Now!




Problem with numbers between 0-100

 

Problem with numbers between 0-100

kingofleinster

5 Nov, 2009 - 08:54 AM
Post #1

New D.I.C Head
*

Joined: 9 Dec, 2008
Posts: 7


My Contributions
Hi Folks

I'm having a problem with the program below , The Problem is that I want it to validate numbers between 0-100 , so I use the '/^[0-100]+$/' , So I call up the webpage , enter in in 75 , It doesn't work keeps throwing up the error "The Marks Must be Between 0 and 100" now here is the unusual bit , if I change the '/^[0-100]+$/' to '/^[0-50]+$/' and call up the page & enter 45 , the program runs fine, why will it not work with '/^[0-100]+$/'.
CODE

<?php

if (isset($_REQUEST['button']))
{

    $error = validate_form();
    if($error)
    {
        display_form_page($error);
    }
    else
    {
        display_output_page();
    }
    
}
else
{
        display_form_page('');
        
}
?>

<?php

function display_form_page($error)
{
    $self = $_SERVER['PHP_SELF'];
    $grade = isset($_REQUEST['sgrade']) ? $_REQUEST['sgrade'] : '';
    ?>


<html>
<head>
<link rel="stylesheet" type="text/css" href="input.css"/>
<title>STICKY MARKS</title>
<style type="text/css">
.error {color: #ff0000}
</style>
</head>
<body>
<h1>Sticky Marks</h1>




<?php
if ($error)
{
    echo "<p>$error</p>\n";
    
}
?>    



<form action="<?php echo $self ?>" method="post">
<p>Please Enter a mark (between 0 and 100);</p>
<p>MARK: <input class="input" type="text" name="sgrade"
    value="<?php echo $grade?>"></p>
  <p><input type="submit" name="button" value="Submit Name"></p>
  </form>
  </body>
  </html>
  <?php
}
?>

<?php
function validate_form()
{
    $grade = trim($_REQUEST['sgrade']);
    $error = '';
    $regxp = '/^[0-100]+$/';
    
        if (! preg_match($regxp,$grade))
        {
            $error .=
            "<span class=\"error\">The Marks Must be Between 0 and 100</span><br>";
        }
        if($grade >= 100)
        {
            $error .=
            "<span class=\"error\">You Must Enter a Number between 0 and 100</span><br>";
        }
        
        return $error;
}
?>

<?php

function display_output_page()
{
    $grade = trim($_REQUEST['sgrade']);
    
?>
<html>
<head>
<title>Grade Result</title>
</head>
<body>
<?php
    echo "A mark of $grade is given to you ";
    
?>    
</body>
</html>
<?php

}

?>


User is offlineProfile CardPM
+Quote Post


girasquid

RE: Problem With Numbers Between 0-100

5 Nov, 2009 - 09:30 AM
Post #2

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,648



Thanked: 58 times
Dream Kudos: 825
My Contributions
Instead of validating [0-100], try validating up to three digits, and then no greater than 100 - using the pattern \d{1,3} to check for between one and three digits.
User is offlineProfile CardPM
+Quote Post

CTphpnwb

RE: Problem With Numbers Between 0-100

5 Nov, 2009 - 11:06 AM
Post #3

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 2,101



Thanked: 155 times
Dream Kudos: 100
Expert In: PHP

My Contributions
Try this:
CODE
$grade = trim($_REQUEST['sgrade']);
if(is_numeric($grade) && $grade <=100 && $grade >=0)
{
    // Valid
} else
{
    // Not valid
}

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 08:38AM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month