Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > HTML > HTML Code Clinic
|
HTML Code Clinic Do you have some HTML code you'd like to share and get suggestions from others for tweaking or improving it? This discussion is the place.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the HTML Code Clinic section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 19th, 2004, 12:05 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default Select Menu Multiple Selections

Hi All,

If you take a look at the html codes below, I know what this does is, it allows us to select multiple options. My question is, is there a way to limit the number of multiple selection a user can make? For example, I want to allow the person to be able to select ONLY 3 options. I tried putting a number in like this: <select name="select" size="4" multiple=3> but it doesn't work.

Is there a way? Thank you.

Leon

<select name="select" size="4" multiple>
<option>Accounting</option>
<option>Administrative Support</option>
<option>Airport</option>
<option>Animal Care</option>
</select>

 
Old July 19th, 2004, 09:48 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

You use JavaScript to validate the selection count.

Code:
<script type='text/javascript'>
    function validate()
    {
        var option_count = 0;

        for (var i = 0; i < document.getElementById('this_field').length; i++)
        {
            if (document.getElementById('this_field').options[i].selected)
            {
                option_count++;
            }
        }

        if (option_count == 0)
        {
            alert('You must enter at least one value');
            return false;
        }
        else if (option_count > 3)
        {
            alert('You selected more than three options.');
            return false;
        }
        else
        {
            return true;
        }
    }
</script>
<form action=''>
    <label for='this_field'>
        Occupations:
    </label>
    <select id='this_field' name='this_field' size='4' multiple='true'>
        <option>Accounting</option>
        <option>Administrative</option>
        <option>Airport</option>
        <option>Animal Care</option>
    </select>
    <input type='submit' name='action' value='Submit' onclick='return validate();'/>
</form>
The validation algorithm checks the count of selected values by accessing the select object via the DOM using the id name of the select field. It checks to see if any selection was made at all, or if more than three values have been selected. It could be just as easily modified to check for only three selections.

The validate() function returns a boolean value, if its true the form submits, if its false the form does not submit.

It should be mentioned that the client-side validation isn't completely foolproof, the user could have javascript disabled.


Regards,
Rich

::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::
 
Old July 20th, 2004, 12:25 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you Rich for your codes and advices! I really appreciate it. I thought there is an easier way beside JavaScript, but I guess not. I will try this out.

Leon






Similar Threads
Thread Thread Starter Forum Replies Last Post
Dropdown list with multiple selections tombert ASP.NET 1.0 and 1.1 Basics 15 March 7th, 2007 07:38 PM
Clear multi select area selections mat41 Javascript How-To 1 November 7th, 2006 01:01 AM
C# ListBox Working With Multiple Selections pro-logic C# 1 October 9th, 2005 02:15 AM
multiple listbox selections written to a text fiel pascoag Access 0 October 14th, 2004 10:01 PM
Cannot store multiple form selections to a mysql d scifo Beginning PHP 2 August 3rd, 2003 04:16 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.