0

"Hi,

I need some help with a text box input. This is used in a form to capture results from exams. I need the user to not be able to put in a amount greater than 100, is there a way of doing this?

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by Catlow
0

oh yes, there is a way, here it is

<form>
/*start from your form*/
<input type='text' name='score' maxlength='3' />
</form>

/* Then in PHP, you validate this */

if (isset($_POST['score'] && !empty($_POST['score'])){
  if($_POST['score'] > 0 && $_POST['score'] <= 100){
   $score = $_POST['score']
   }
  else{
    echo "Value entered is incorrect";
  }
else{
 echo "Enter your score in the form below";
 //and blah blah
}

What this does first of all is to check whether the input is set and its not empty, if passes that, it further checks whether is greater than zero and also less or equal to 100.
Your form will accept only three characters but that is not to say that a user cant post 999 as his score. The condition checks this then.

0
<html>
<head></head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Enter your mark <input name="score" type="text" size="3"> 
<input type="submit" name="submit" value="Sub">
</form>

<?php

if (isset($_POST['submit'])) {
    $score = $_POST['score'];
	if (!empty($score)) {
		if ($score > 0 && $score <= 100) {
			echo "Validation Process Over";
		}
		else echo "Type the numeric value";
	}
}
?>
</body>
</html>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.