Kreator
Kreator

Reputation: 15

How to validate select with PHP?

How to validate day/month/year select with php? I red some topics about who to validate select with 2-3 or even 5-6 select options. I would know how to validate that kind of select. But in the case of, for exemple, month, there are 32 options. Or in the case of year option, the are 60-70 options. So im interested in whats the optimal way to do select validation when you have so much options?

Upvotes: 0

Views: 3766

Answers (2)

crush
crush

Reputation: 17013

Validating selects is fairly trivial.

Year

Let's say you have a sequential range of years from 1960-2013.

Your HTML for the selects is:

<select name="year">
    <option value="1960">1960</option>
    ...
    <option value="2013">2013</option>
</select>

So, you know that your year has to fall in the range of 1960 to 2013:

$year = (int) $_POST["year"]);

if ($year < 1960 || $year > 2013) {
    $validationPassed = false;
}

Simple enough to validate the year.

Month

Now that you know what year it is, you can find out if it is a leap year. However, that is only important for a single month, so first let's validate what month they selected:

<select name="month">
    <option value="0">January</option>
    ...
    <option value="11">December</option>
</select>

Once again, validation of the month is easy. We know it has to be a value between 0 and 11.

$month = (int) $_POST["month"];

if ($month < 0 || $month > 11) {
    $validationPassed = false;
}

Day

Now we know the month and the year. to validate the selected day, we need to check the following criteria:

  1. Is it a leap year?
  2. Is it the month of the year with the extra day for the leap year? (February).

For a year to be a leap year, it has to be divisible by 400, or not divisible by 100 and divisible by 4:

function isLeapYear($year) {
    if ($year % 400 == 0)
        return true;
    else if ($year % 100 == 0)
        return false;
    else if ($year % 4 == 0)
        return true;

    return false;
}

Now, we can check if it is a leap year or not. If it is a leap year, then we know we need to allow 1 extra day in February.

We make an array of days in each month:

$daysInMonth = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

Then we get the amount of days in the current month:

$monthDays = $daysInMonth[$month];

If it is a leap year, we add a day to the total days in the month:

if ($month == 1 && isLeapYear($year)) //February
    $monthDays++;

Finally, we can validate the day

$day = (int) $_POST["day"];

if ($day < 0 || $day > $monthDays) {
    $validationPassed = false;
}

Now, all of this is basically encapsulated into the checkdate() function that was in the other answer below. So, to use checkdate() instead of all the code above, you could simply do the following:

HTML (Note: checkdate() is not zero-based, so we start our days and months at 1 instead of 0.)

<form action="myphp.php" method="post">
    <select name="day">
        <option value="1">1</option>
        ...
        <option value="31">31</option>
    </select>

    <select name="month">
        <option value="1">1</option>
        ...
        <option value="12">12</option>
    </select>

    <select name="year">
        <option value="1960">1960</option>
        ...
        <option value="2013">2013</option>
    </select>
</form>

PHP

$day = (int) $_POST["day"];
$month = (int) $_POST["month"];
$year = (int) $_POST["year"];

$validationPassed = checkdate($month, $day, $year);

Upvotes: 1

zerkms
zerkms

Reputation: 254896

For date it's actually relatively easy - you just use checkdate().

And for the other cases you could check if the value is within an expected range like:

if ($val >= min && $val <= max)

or, in case you have all the values in an array

if (in_array($val, $array_of_valid_values))

Upvotes: 2

Related Questions