Example

PHP

//******************************************
//******************************************
//********** VALIDATE INPUT - INT **********
//******************************************
//******************************************
function ValidateInputInt($FieldName, &$Error, $OptionsArray)
{
  if ($_SERVER["REQUEST_METHOD"] == "POST")
  {
    if ( ($EnteredValue = filter_var($_POST[$FieldName], FILTER_VALIDATE_INT, $OptionsArray)) !== False )
    {
      return($EnteredValue);
    }
    else
    {
      $Error = "* Invalid";  //Or could use "* Invalid ${FieldName}";
      return False;
    }
  }
  else
  {
    return False;
  }
}


$MyInputName_Options =   ["options" => [
    "min_range" => 1,
    "max_range" => 12
  ]];
$MyInputName = ValidateInputInt("MyInputName", $MyInputName_Error, $MyInputName_Options);

HTML

  Enter value: <input type="number" name="MyInputName" value="<?= $_POST["MyInputName"];?>" min="<?=$MyInputName_Options["options"]["min_range"];?>" max="<?=$MyInputName_Options["options"]["max_range"];?>" >
  <span class="error"><?= $MyInputName_Error;?></span>
  <br>
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *