469,572 Members | 1,205 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,572 developers. It's quick & easy.

java script to validate combobox

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
2 3944
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
<bb****@yahoo.comwrote in message
news:11**********************@e65g2000hsc.googlegr oups.com...
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 5 '07 #2
<bb****@yahoo.comwrote in message
news:11**********************@e65g2000hsc.googlegr oups.com...
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%>").f ocus();
return false;
}

--
http://www.markrae.net

May 5 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by NNTP | last post: by
3 posts views Thread by hoz | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.