We help IT Professionals succeed at work.

Javascript: check if selection has been made on Drop_down box(Mult-selection)

mrong
mrong asked
on
I need a Javascript function to check any selection has been made on Drop_down box in asp.
Thanks.
Comment
Watch Question

Commented:
IF(Formname.dropdown.selectedindex < 0)
   //nothing selected
ELSE
   //selected an item
   Formname.dwopdown.options[formname.dropdown.selectedindex].value

Author

Commented:
I am trying to force the user make selection on the dropdown list like the following, but the form is submitted without a selection.

function VerifyData()
  {
 if(document.frmUser.ProjectType.dropdown.selectedindex < 0 )
    {
     alert("Project Type is a required field,and can't be empty.");
     return false;
    }          
else
      return true;
  }

 <FORM ACTION="Send.asp" NAME="frmUser" METHOD="POST"
              onSubmit="return VerifyData()">


Commented:
function VerifyData()
  {
 if(document.frmUser.ProjectType.dropdown.selectedIndex < 0 )
    {
     alert("Project Type is a required field,and can't be empty.");
     return false;
    }          
else
      return true;
  }


Javascript is case sensitive. selectedIndex (I upper case)

Author

Commented:
I corrected as the following but still not working.
function VerifyData()
  {
 if(document.frmUser.ProjectType.dropdown.selectedIndex < 0)
    {
      alert("Project Type is a required field,and can't be empty.");
     return false;
    }  
else
      return true;
  }

Commented:
Hi

 try to display the selectedIndex before the IF condition.

function VerifyData()
  {
alert(document.frmUser.ProjectType.dropdown.selectedIndex);
 if(document.frmUser.ProjectType.dropdown.selectedIndex < 0 )
    {
     alert("Project Type is a required field,and can't be empty.");
     return false;
    }          
else
      return true;
  }

Commented:
If nothing works, try changing this line

 <FORM ACTION="Send.asp" NAME="frmUser" METHOD="POST"
              onSubmit="return VerifyData();">

Put semicolon after VerifyDate();

Explore More ContentExplore courses, solutions, and other research materials related to this topic.