Avatar of trevor1940
trevor1940
 asked on

PHP: Best practice for validating Option Select

I have a html form with an option select for choosing a country name see sample code bellow

I'm wondering how to validate this part of the form in PHP using filter input

filter_input(INPUT_POST, "Country", FILTER_VALIDATE_INT);

Will detect if no country has been selected because the value is 1
how would I validate a proper 3 figure country code

Is regex the best way or test for string length?  


<div >
 <select name="Country" >
  <option selected value=1>Please select</option>
  <option value="BRA">Brazil</option>
  <option value="NZL">New Zealand</option>
  <option value="SWZ">Switzerland</option>
 </select>
</div>

Open in new window

HTMLPHP

Avatar of undefined
Last Comment
trevor1940

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Steve Bink

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Julian Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Vijaya Kumar

Hope this helps  


  if (is_numeric($POST["Country"])) {
        echo "No country selected";
    } else {
      $country = array("brazil", "new land", "xxxx", "yyy");
      if (in_array($_POST["Country"], $country)) {
           echo "Got it";
        }
       else
      {
         echo "no";
      }
    }
trevor1940

ASKER
Thanx for your suggestions
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes