Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java Script for a Combo Box

Status
Not open for further replies.

aumstu

MIS
Jan 20, 2006
40
US
I am trying to validate if a combo box has been selected..
Here is the code


<script type="text/javascript">

function validate_required(field,alerttxt)
{
with (field)
{

if (value=="[Select]")
{alert(alerttxt);return false}
else {return true}
}
}

function validate_form(thisform)
{
with (thisform)
{

if (validate_required(Department,"Please Select a Department")==false)
{Department.focus();return false}



}
}
</script>

The Code works great for a textbox....But this combo box does nothing. The name of the Combo box is Department and the default value is [Select] I also, addded the code onsubmit="return validate_form(this)" where the form name is.

Thanks for any help
 
That's because you have to access the value through the selected index:

with (field[field.selectedIndex])
{
if (value=="[Select]")
{alert(alerttxt); return false}
else {return true}
}

Lee
 
Thanks for answering....I ended up using the following code.

<SCRIPT LANGUAGE="JavaScript">
<!--
function validateForm(){
if(document.ItemList.Department.selectedIndex==0)
{
alert("Please select your latest graduate degree.");
document.ItemList.Department.focus();
return false;
}
return true;
}
//-->
</SCRIPT>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top