Link to home
Create AccountLog in
Avatar of mrong
mrong

asked on

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

I need a Javascript function to check any selection has been made on Drop_down box in asp.
Thanks.
Avatar of rajvja
rajvja
Flag of United Kingdom of Great Britain and Northern Ireland image

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

ASKER

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()">


ASKER CERTIFIED SOLUTION
Avatar of rajvja
rajvja
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of mrong

ASKER

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

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;
  }
If nothing works, try changing this line

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

Put semicolon after VerifyDate();