Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: validation on listboxes


Message #1 by "Alexander Bienz" <alex@v...> on Tue, 28 Aug 2001 17:35:16
Sorry I forgot that it was a multiple text box :)
This WILL work, promise :)

function validcat ( )
{
   var form = document.form1;
   var valid = false;
   for ( var i = 1; i < form.mylistbox.length; i++ )
   {
       if ( form.mylistbox[i].selected == true )
       {
           valid = true;
       }
   }
   if ( valid ) form.submit ( );
   else alert ( "Please select something" );
}

I've used all your old names, you don't need to change anything, just
replace the old function directly with this one. I've also made sure it does
exactly the same thing as your old function tried to do, either alerts the
user if none of the options are selected, or else submits the form. That way
we don't have to go into how you're calling it and that sort of thing (It
seems like a strange way to validate though, submitting the form inside the
function like that. Unless the list box is the only thing in the form and
submitting the form straight away after you're happy the user has selected
something doesn't matter, you won't be able to validate any controls after
the select box, and any invalid input before it might still be submitted,
depending on your code of course.)
You don't need the Please Select option for multiple select boxes, so you
probably should remove it. If you do make sure you start the loop at 0
instead of 1, everything will still work the same.

-- 
Donncha Fahy


From: "Alex Bienz\(viplondon\)" <alex@v...>
Reply-To: "javascript" <javascript@p...>
Date: Fri, 31 Aug 2001 09:05:43 +0100
To: "javascript" <javascript@p...>
Subject: [javascript] Re: validation on listboxes


Yes, but if I select more than one value from my LISTbox, that will still
return the alert if the 'Please Select' value is one of them, won't it?

> -----Original Message-----
> From: Donncha Fahy [mailto:fahydonncha@h...]
> Sent: 30 August, 2001 9:33 PM
> To: javascript
> Subject: [javascript] Re: validation on listboxes
>
>
> This will work:
>
> function validcat()
> {
> if(document.form1.mylistbox.selectedIndex == 0 ){alert('Please select
> something);}
> else{document.form1.submit();}
> }
>
> Make sure that "0" is the index position of the "Please
> Select" option i.e.
> 1st in the list, or edit accordingly.
>
> --
> Donncha Fahy
>
>
> From: "Alexander Bienz" <alex@v...>
> Reply-To: "javascript" <javascript@p...>
> Date: Tue, 28 Aug 2001 17:35:16
> To: "javascript" <javascript@p...>
> Subject: [javascript] validation on listboxes
>
>
> Hi Chaps,
>
> I've got a symple bit of validation that I'm trying to
> implement into an
> asp page, but it's not returning the desired results, perhaps there's
> someone out there that could help me work this one out.
>
> My function ATM is...
>
> function validcat(){
> if(document.form1.mylistbox.value=='Please
> Select'){alert('Please select
> something);}
> else{document.form1.submit();}
> }
>
> Right, I have a list box with about 50 different values, and
> the initial
> value says 'Please select'  I don;t want the user to submit
> the form with
> only this selected, but I do want the user to be able to
> submit the form
> if something else is selected aswell, via the ctrl button and
> clicking.
>
> However whenever I try and run the function I will always get
> my alert box
> as long 'Please Select' is selected.
>
> Is there a way around this?
>
> cheers,
>        Al.

  Return to Index