Ensuring a listbox has at least one value in it

S

Simon

Hi

Does anyone know how I can be sure that a listbox has a value in it without
doing a post back?

If I attach a required field validator it will only allow the page to submit
if I actually highlight one of the items in the listbox to validate.

I'm really hoping that this can be done somehow using the required field
validator. I've looked at the custom validator control but that looks like
way to much hassle especially seeing as I don't know javascript

Thanks to anyone who can help

Simon
 
R

Raterus

Well you are creating the contents for the listbox from your server-side script right? Can't you check it there? Unless the user is dynamically adding values to this listbox, which if that is the case, you probably created that, and therefore you know just a little about javascript :). I don't see how a requiredfieldvalidator would work in this case, if they don't actually have to select a value. You'll have to make your own javascript to check this, shouldn't be too hard.
 
V

Vidar Petursson

Hi,

For multiple you can
var e = document.forms[0].SELECTNAME;
var b = 0;
for(i=0;i<e.length;i++)
{
if(e.options.selected) b++;
}
if(!b) alert("Select something");


First option a no go
if(e.selectedIndex < 1 ) alert("Select something");

More info
http://msdn.microsoft.com/library/d...hor/dhtml/reference/dhtml_reference_entry.asp

--
Best Regards
Vidar Petursson
==============================
Microsoft Visual: Scripting MVP 2000-2004
http://www.icysoft.com/
http://www.deus-x.com/ Instant e-commerce
http://www.microsoft.com/technet/scriptcenter/
Playground: http://213.190.104.211/ ( IE 5.5+ only )

No matter where you go there you are
==============================
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top