Welcome Guest, Not a member yet? Register   Sign In
How to validate dropdown/selct data
#1

[eluser]RS71[/eluser]
Hey

For example, say I had a drop down to choose between male or female or a list of states:

How would I check that a valid option is sent? (Check if data is in a certain array?)

Thanks in advance
#2

[eluser]spyro[/eluser]
The most secure way that I know of is to create an array on the backend that matches the options in the HTML. Then when the form is submitted, run through the array and make sure that it is a valid option.

If you manually populate both the HTML drop down and the array then you will have to make sure to keep them in sync. One way to overcome this is to populate both the drop down and valid options array with the same array from a metadata file or set of values from the database.
#3

[eluser]Sumon[/eluser]
using a custom validation may be a good choice. by example in form_validation.php add a validation rule
Code:
$this->form_validation->set_rules('sel_sex', 'sex', 'required|callback_check_sex');
and the function check_sex code
Code:
function check_sex()
{
   $valid_selections = array('m','M','f','F');
   $x = $_POST['sel_sex'];
   if(in_array($x , $valid_selections))
   {
     return true;
   }
   else
   {
     $this->form_validation->set_message('check_sex', 'invalid choice');
     return FALSE;
   }
}




Theme © iAndrew 2016 - Forum software by © MyBB