firestarter30 Posted March 21, 2011 Share Posted March 21, 2011 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 More sharing options...
nogray Posted March 22, 2011 Share Posted March 22, 2011 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 } Link to comment https://forums.phpfreaks.com/topic/231332-dropdown-menu-validation/#findComment-1190628 Share on other sites More sharing options...
firestarter30 Posted March 22, 2011 Author Share Posted March 22, 2011 It worked like a charm nogray , thanks a lot :D :D Link to comment https://forums.phpfreaks.com/topic/231332-dropdown-menu-validation/#findComment-1190655 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.