java script to validate combobox

B

bbawa1

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

Scott M.

A drop down list must always have something selected, so usually you make
the first item in the list say something like --- Please Choose From
Below --- and then check to see if the first item is still selected at the
time of submission:

if(ddlDrop.selectedIndex == 0)
{
//No item was selected
}
else
{
//Something other than item 1 was selected
}

Also, in your other code (below), there is no need to refer to your controls
via:

document.getElementById("<%=txtUserName.ClientID%>").value

just write:

txtUserName.value

-Scott
 
M

Mark Rae

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.

Assuming you have a blank option as the first option in the DropDownList:

if(document.getElementById('<%=ddlDrop.ClientID%>').selectedIndex == 0)
{
alert("Please Pprovide DropDown");
document.getElementById("<%=ddlDrop.ClientID%>").focus();
return false;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,278
Latest member
BuzzDefenderpro

Latest Threads

Top