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
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
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.