# Allow selecting only two items in multiple selection box?

**URL:** <https://www.ruby-forum.com/t/allow-selecting-only-two-items-in-multiple-selection-box/138379>\
**Category:** Rails\
**Created:** [May 22, 2008, 1:52pm UTC](https://www.ruby-forum.com/t/allow-selecting-only-two-items-in-multiple-selection-box/138379 "2008-05-22T13:52:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pepa007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/779978/32.png) [@pepa007](https://www.ruby-forum.com/u/pepa007)\
**Post date:** [May 22, 2008, 1:52pm UTC](https://www.ruby-forum.com/t/allow-selecting-only-two-items-in-multiple-selection-box/138379/1 "2008-05-22T13:52:10Z")

</div>

Hi,

I’d like to use ‘selection’ with :multiple=\>true but allow selecting  
only two items from the list. is there any easy way in RoR or I have  
to use some javascript testing with onChange event?

Thanks very much

---

<div class="post-metadata">

**Author:** ![pepa007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/779978/32.png) [@pepa007](https://www.ruby-forum.com/u/pepa007)\
**Post date:** [May 22, 2008, 3:10pm UTC](https://www.ruby-forum.com/t/allow-selecting-only-two-items-in-multiple-selection-box/138379/2 "2008-05-22T15:10:27Z")

</div>

If you want to give the user feedback immediately when he selects a  
second option, you need to use Javascript. It can on the other hand be  
generated by RoR using RJS (called by a remote\_function), but it it  
probably easier just to put it directly in the onchange-handler as you  
suggested.

---

<div class="post-metadata">

**Author:** ![pepa007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/779978/32.png) [@pepa007](https://www.ruby-forum.com/u/pepa007)\
**Post date:** [May 22, 2008, 7:49pm UTC](https://www.ruby-forum.com/t/allow-selecting-only-two-items-in-multiple-selection-box/138379/3 "2008-05-22T19:49:04Z")

</div>

On 5/22/08, pepa007 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’d like to use ‘selection’ with :multiple=\>true but allow selecting  
> only two items from the list. is there any easy way in RoR or I have  
> to use some javascript testing with onChange event?

[http://www.webtricks.com/sourcecode/source\_code.cfm?CodeID=16](http://www.webtricks.com/sourcecode/source_code.cfm?CodeID=16)

(3 seconds on google)

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)
