Jump to content

DropDown Menu Validation


firestarter30

Recommended Posts

Hi all ,

something short i would like to validate a drop down menu , here is the html code

 

<label for="Website type">Website Type</label>
<select name="dropdown">
<option selected="selected">Select A Category</option>
<option value="sites">Sites</option>
<option value="directories">Directories</option>
<option value="blogs">Blogs</option>
</select>

 

From the javascript part i tried to validate with this :

var dropdown = $('form #dropdown').val();

if(dropdown =='Select A Category'){
valid = '<p>A valid website choice ' + required +'</p>';
}
//I tried also with (dropdown =='Select A Category') , (dropdown ='')  but didnt worked either.

 

What im missing here?

PS: Just for the history , when i was trying to validate the dropdown menu in php , i was assuming that because the "Select A Category" hasnt assinged any value , it would be empty, but after i got suspicious and echo the menu just to see what is happening it echoed Select A Category and when validating like this

if ($dropdown == 'Select A Category')  {
$error_message .= "<p>Please choose your website type.</p>";				
}

it was working as expected. But is javascript this doesnt work.

Link to comment
https://forums.phpfreaks.com/topic/231332-dropdown-menu-validation/
Share on other sites

You would need to add a value="" to the Select a Category and add an ID to the select menu

<select name="dropdown" id="dropdown"><option selected="selected" value="">Select A Category</option>

and test for value == ""

// I don't use jQuery
if (document.getElementById('dropdown').value == "") {
// do something
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.