Filter_var: Why won't this work?

Can’t figure out why this won’t work. It’s part of the sa sanitizing code for the Bobby Tables project.

You’re taking in a birth year and making sure it conforms to some reasonable time range. The Hint plugs in the year directly and compares it to date(“Y”).
The method I tried to use was create a new variable, age which would be equal to date(“Y”) - _raw_birth_year. Then using 5 and 120 as the min-range and max-range. But it doesn’t work. The program doesn’t crash, but it allows any year to go through.

  $raw_birth_year = $_POST["birth_year"];
  $age = date("Y") - $raw_birth_year;
  $options = ["options" => ["min-range" => 5, "max-range" => 120]];
  if (filter_var($age, FILTER_VALIDATE_INT, $options)) {
    $birth_year = $raw_birth_year;
  } else {
    $validation_error .= "That can't be your birth year. <br>";

Hey there!

Try "min_range" and "max_range" instead.

Let me know if that helps.

1 Like

oh, dumb I just messed up with the - vs _? Couldn’t figure that one out! Thanks!

1 Like