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?

8 Years
Discussion Span
Last Post by Catlow

oh yes, there is a way, here it is

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

/* Then in PHP, you validate this */

if (isset($_POST['score'] && !empty($_POST['score'])){
  if($_POST['score'] > 0 && $_POST['score'] <= 100){
   $score = $_POST['score']
    echo "Value entered is incorrect";
 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.

<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">


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";
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.