-2

I recently started writing a website for learning html/css/js/php.

I designed the front-end of my site with bootstrap. Now I am trying to validate some inputs with PHP.

I tried this:

    if ($durationHH <= 0 && $durationMM <= 0)
    {
        echo "DurationHH and DurationMM can not be both zero at the same time.";    
        echo "<br>";
        echo "DurationHH and DurationMM can not be smaller than 0.";       
    }
    elseif (empty($durationHH) || empty($durationMM))
    {       
        echo "DurationHH and DurationMM can not be empty.";
        echo "<br>";
    }
    else
    {
        echo $_POST["durationMM"];
        echo ":";
        echo $_POST["durationHH"];
    }

I tested the validation by putting in some values for durationHH and durationMM.

Everything is working fine so far, except these two cases:

durationMM = 0 AND durationHH = any value

&&

durationHH= 0 AND durationMM = any value.

In these two cases i get the output: "DurationHH and DurationMM can not be empty."

How/why does this happen?

1 Answers1

0

if durationHH and durationMM are always required to be integers or numeric, use ! is_numeric() to check.

Andrew Nolan
  • 1,987
  • 2
  • 20
  • 23