0

I have a form that contains a number of textboxes i.e. Volome, Gain, Treble, Middle and Bass. Only whole numbers can be entered, which is validated with javascript and the Maxlength is set to, so no problem there. But how do I make sure that only numbers between 0 and 65535 are entered.

<?php
        $name = $_POST['ampMod'];
        $volume = 'Volume = '. $_POST['volume'];
        $gain = 'Gain = '. $_POST['gain'];
        $treble = 'Treble = '. $_POST['treble'];
        $middle = 'Middle = '. $_POST['middle'];
        $bass = 'Bass = '. $_POST['bass'];

if($volume != null && $gain != null && $treble != null && $middle != null && $bass != null)
{

        echo "<h3> $name </h3>";
        echo "<table><tr>";
        echo "<td>$volume</td>";
        echo "<td>$gain</td>";
        echo "<td>$treble</td>";
        echo "<td>$middle</td>";
        echo "<td>$bass</td>";
}
else
{echo ("Please try again. Values must be between 0-65535. 0=Off 65535=Full On 10<br><a href = \"ampchoice.php\">Click here to try again!</a>");}
?>

5 Answers 5

0

It is important to mention that your $volume, $gain, $treble, $middle and $bass will never actually be null as you have assigned a string to them in addition to the $_POST value. In addition you should always check if the $_POST values exist before trying to use them (or you will get an undefined notice message).

Here is an example for a PHP version based on the code you had (untested, but should work fine).

<?php

function isValidRange( $value, $low = 0, $high = 65535) {
  // validate / cast value as int (add additional validation here
  $value = (int)$value;
  if ( $value > $high || $value < $low ) {
    // return null (not a valid value)
    return null;
  }
  // otherwise the value is valid so return it
  return $value; 
}

// make sure the $name var is safe to use
$name = ( isset($_POST['ampMod']) ) ? htmlentities($_POST['ampMod'],ENT_QUOTES,'UTF-8') : null;
$volume = ( isset($_POST['volume']) ) ? isValidRange($_POST['volume']) : null; 
$gain = ( isset($_POST['gain']) ) ? isValidRange($_POST['gain']) : null; 
$treble = ( isset($_POST['treble']) ) ? isValidRange($_POST['treble']) : null; 
$middle = ( isset($_POST['middle']) ) ? isValidRange($_POST['middle']) : null; 
$bass = ( isset($_POST['bass']) ) ? isValidRange($_POST['bass']) : null; 

if( isset($volume) && isset($gain) && isset($treble) && isset($middle) && isset($bass) )
{

        echo "<h3> $name </h3>";
        echo "<table><tr>";
        echo "<td>Volume = $volume</td>";
        echo "<td>Gain = $gain</td>";
        echo "<td>Treble = $treble</td>";
        echo "<td>Middle = $middle</td>";
        echo "<td>Bass = $bass</td>";
        echo "</tr></table>";
} else {
  echo ("Please try again. Values must be between 0-65535. 0=Off 65535=Full On 10<br><a href = \"ampchoice.php\">Click here to try again!</a>");}
?>

Lastly I would not recommend just relying on JavaScript to actually check if your values are safe to use (i.e. echo them out), but using js as a pre-warning to users and then properly validating with PHP is the best way to go.

0

Just do something like this? Don't know why you would want to go between 0 and 65535. I doubt you want them to go that high. If you do just change 10 to 65535

if($value > 10 || $value < 0)
{
    echo "Value cant be higher then 10 or lower then 0";
}

This makes sure the value is between 10 and 0

2
  • The reasoning for the range is because eventually these values will be used in an xml file and saved as an amp preset. The presets are for Fender Fuse software. I wish I knew why?
    – redbox3
    Jun 11, 2012 at 15:51
  • No just seemed like such a random value to me, but then it's fine ;)
    – Bono
    Jun 11, 2012 at 19:13
0

In situations like this, I often prefer to silently clean the form input. You've got client-side validation in place already. If the value is higher than allowed, just set the value to the maximum allowed instead of showing an error message.

// Clean the posted data and prevent notices if not set
$volume = (isset($_POST['volume'])) ? (int) $_POST['volume'] : 0;

// Make sure the value is within a certain range
$min = 0;
$max = 10;
$volume = min($max, max($min, $volume));
0

You can make use of the filter extension (bundled by default since 5.2):

$FILTER_VALIDATE_KNOB = array(
        'filter' => FILTER_VALIDATE_INT,
        'options' => array(
                'min_range' => 0,
                'max_range' => 65535,
        )
);

$res = filter_input_array(INPUT_POST, array(
  'ampMod' => $FILTER_VALIDATE_KNOB,
  'volume' => $FILTER_VALIDATE_KNOB,
  'gain' => $FILTER_VALIDATE_KNOB,
  'treble' => $FILTER_VALIDATE_KNOB,
  'middle' => $FILTER_VALIDATE_KNOB,
  'bass' => $FILTER_VALIDATE_KNOB,
));

if (is_null($res) || in_array(null, $res, true)) {
    // some or all fields are missing
    // - missing fields have null value
} elseif (in_array(false, $res, true)) {
    // some or all fields have a wrong value
    // - wrong values have false value
}
0

I would do it with javascript. That way, you wouldn't have to submit the form and if the user types a higher number the alert (or something nicer) is shown:

In the input field, just call the javascript function:

<input id="thefirstnumbervalue" type="text"  onchange="checknumber('thefirstnumbervalue')" />

<input id="thesecondnumbervalue" type="text"  onchange="checknumber('thesecondnumbervalue')" />

In the function:

function checknumber(theid){
var mynumbervalue = document.getElementById(theid).value;
if (mynumbervalue > 65535){

document.getElementById(theid).value = "65535";

alert("Please try again. Values must be between 0-65535. ...");
}

if(mynumbervalue < 0){

document.getElementById(theid).value = "0";

alert("Please try again. Values must be between 0-65535 ...");
}
}    

This is a simple approach in raw javascript. If you use ajax and jquery the result could be easier and nicer. This is complementary to the php solution, as you should also check the data before inserting in your database.

3
  • Thanks, but it only seems to work on the first input, not sure what i'm doing wrong??
    – redbox3
    Jun 11, 2012 at 17:52
  • I wrote this code thinking in just one input, that's why I used the "id" as a fixed value in function. If you use more than one input, you can't use the same id. So change the id, and pass the id as a variable to the function. I am going to edit my answer to reflec it.
    – Jorge
    Jun 11, 2012 at 22:06
  • I've been staring at this for too long and completely overlooked the obvious... Of course they need to be unique! Thank you again for your help.
    – redbox3
    Jun 12, 2012 at 0:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.