...

View Full Version : Check if dropdown is selected



holty
10-14-2003, 02:44 PM
Hi,

I have this code to check if a text box has been filled in

function validRequired(formField,fieldLabel)
{
var result = true;

if (formField.value == "")
{
alert('Please enter a value for the "' + fieldLabel +'" field.');
formField.focus();
result = false;
}

return result;
}

Its called by:

if (!validRequired(theForm.Name,"Name"))
return false;

How can I change it to work for a dropdown?

I have tried:

if (formField.SelectIndex.value == "")

but it didn't work :(

any clues?

vids
10-14-2003, 03:16 PM
To access or check the value in a drop down use the following convention:

if (fieldName.options[fieldname.selectedIndex].value=="") {

do something......

}

I hope this helps :thumbsup:

holty
10-14-2003, 03:41 PM
thanks - but how could you put it into a function?

I have loads of dropdown boxes you see -- I've been trying to modify the function in my previous post but no joy :(

vids
10-14-2003, 03:47 PM
You can write a function just like the one you started of with:

function validRequired(formField,fieldLabel,fieldType)
{
var result = true;

if (fieldType == "dropDown") {
if (formField.options[formField.selectedIndex].value == "")
{
alert('Please select a value from the the drop down list "' + fieldLabel +'".');
formField.focus();
result = false;
}

if (fieldType == "text") {
if (formField.value == "")
{
alert('Please enter a value in the "' + fieldLabel +'" field.');
formField.focus();
result = false;
}

}

}

return result;
}



When calling the function you will need to pass it the type of field that you want to validate.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum