Link to home
Start Free TrialLog in
Avatar of mrong
mrong

asked on

DropDown box in ASP

Greeting,

I have the a dropdown box in asp and it populates data from a database as the following.

<TD width="414" colspan="3" style="height: 23px">
            <SELECT  name="Bldg"  style="width: 536px">
      <%call  getBLDG%>
</SELECT></TD>

I use the following javascript to make the field required before  regardless if the user selects item from dropdown list or not, the form always popup message "Building One is a required field,and can't be empty." Please suggest.

  if(isWhitespace(document.frmUser.Bldg.value,true))
    {
     alert("Building One is a required field,and can't be empty.");
     return false;
    }
Avatar of Big Monty
Big Monty
Flag of United States of America image

change your javascript to:

 if(isWhitespace(document.frmUser.Bldg.options[document.frmUser.Bldg.selectedIndex].value == '' ))
    {
     alert("Building One is a required field,and can't be empty.");
     return false;
    }
Curious as to what the function isWhitespace does. But if you are checking to see if the field has been filled out, the code below should suffice.
  if(document.frmUser.Bldg.value == '')
    {
     alert("Building One is a required field,and can't be empty.");
     return false; 
    }

Open in new window

Avatar of mrong
mrong

ASKER

Big Monty,

it doesn't work.
Thanks.
you actually shouldnt need the isWhitespace function, as you control all of the values in the dropdown, I would remove it and do:

 if(document.frmUser.Bldg.options[document.frmUser.Bldg.selectedIndex].value == '' )
    {
     alert("Building One is a required field,and can't be empty.");
     return false;
    }
ASKER CERTIFIED SOLUTION
Avatar of Big Monty
Big Monty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Does one of your options include a value?  If your page renders option values, something is chosen on submit.  The first value is the default unless.  I would make the first value something you can check.  Also, js can be disabled so you want to check this serverside and make sure you are accepting a value that is in the proper range.

 <SELECT  name="Bldg"  style="width: 536px">
      <!-- < % call  getBLDG % > -->
   <option value="0">No Option Selected</option>
 <option value="1">Option 1</option>
 <option value="2">Option 2</option>
</SELECT>

Open in new window