By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,012 Members | 988 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,012 IT Pros & Developers. It's quick & easy.

java script to validate combobox

P: 6
have to validate my web page using java script. I have one dropdown list also . I need a javascript to validate weather any item in dropdownlist is selected or not. e.g ddlDrop is my dropdownlist.

To validate other controls I am using following
function validate()
{
if (document.getElementById("<%=txtUserName.ClientID% >").value=="")
{
alert("Please Pprovide UserName");
document.getElementById("<%=txtUserName.ClientID%> ").focus();
return false;
}
if (document.getElementById("<%=txtPassword.ClientID% >").value=="")
{
alert("Please provide Password");
document.getElementById("<%=txtPassword.ClientID%> ").focus();
return false;
}
May 4 '07 #1
Share this Question
Share on Google+
2 Replies


acoder
Expert Mod 15k+
P: 16,027
I assume the user is not allowed to select the first item in the list.

You can use the selectedIndex property. Check if it's 0. If it is, that means the first item is selected.
May 5 '07 #2

pbmods
Expert 5K+
P: 5,821
You can check the selected index of a select by using:

Expand|Select|Wrap|Line Numbers
  1. document.getElementById('theElement').selectedIndex
  2.  
You can check the value of the selected option by using:

Expand|Select|Wrap|Line Numbers
  1. var theElement = document.getElementById('theElement');
  2. theElement.options[theElement.selectedIndex].value
  3.  
http://www.w3schools.com/htmldom/dom_obj_select.asp
http://www.w3schools.com/htmldom/dom_obj_option.asp
May 5 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.