View Full Version : Counting number of selections in dropdown
Jimbo9
01-06-2003, 02:14 PM
I have a form with a multiple choice dropdown - i.e. you can select (say) 6 items from a list of 20 - but I want to validate on submission that no more than 6 have been selected.
Q1: How do I use string.length or array.length to validate?
Q2: How do I convert a string into an array in order to use (say Array.length to validate?
beetle
01-06-2003, 03:10 PM
Here is a slightly re-worked version of the validation function used in my fValidate (http://www.peterbailey.net/fValidate) script.function validateSelectM( formObj, minS, maxS )
{
var selectCount = 0;
if ( maxS == 999 ) maxS = formObj.length;
for (var i = 0; ( o = formObj.options[i] ); i++)
{
if ( o.selected )
selectCount++;
}
if ( selectCount < minS || selectCount > maxS )
{
// Error notification here
return false;
}
return true;
}And to answer your questions directly
Q1)
Look above
Q2)
var myString = "Hello";
var myArray = myString.split('');
// myArray == ['H','e','l','l','o']
Jimbo9
01-06-2003, 05:51 PM
Hi beetle!
Thanks for the pointer - rating ACE! ;-)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.