# Limit number of selections in multiple select

**URL:** <https://www.ruby-forum.com/t/limit-number-of-selections-in-multiple-select/184017>\
**Category:** Rails\
**Created:** [February 23, 2010, 10:32pm UTC](https://www.ruby-forum.com/t/limit-number-of-selections-in-multiple-select/184017 "2010-02-23T22:32:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [February 23, 2010, 10:32pm UTC](https://www.ruby-forum.com/t/limit-number-of-selections-in-multiple-select/184017/1 "2010-02-23T22:32:07Z")

</div>

How can I limit the number of selections in multiple select? Say, a  
user should not be able to select more than 5 values in a multiple  
selection box. I am following the multiple select example here:  
[http://www.gilluminate.com/2007/02/15/best-way-to-do-multiple-select-combo-boxes-in-rails/](http://www.gilluminate.com/2007/02/15/best-way-to-do-multiple-select-combo-boxes-in-rails/)  
. Thanks.

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [February 24, 2010, 12:14pm UTC](https://www.ruby-forum.com/t/limit-number-of-selections-in-multiple-select/184017/2 "2010-02-24T12:14:38Z")

</div>

I don’t think you can with Rails (well, you could do it with validation  
on  
the model) but it’s easy enough with jQuery.

Assuming you have a text box like this:

option1 option2 option3 option4 option5 option6 option7

You can do this:

Or take whatever action you want, but basically $(sel).val() returns an  
array for multiple select boxes.

Cheers,

Andy

–  
Andy J.  
[http://andyjeffries.co.uk/](http://andyjeffries.co.uk/)#rubyonrails #mysql #jquery  
Registered address: 64 Sish Lane, Stevenage, Herts, SG1 3LS  
Company number: 5452840

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [February 24, 2010, 12:26pm UTC](https://www.ruby-forum.com/t/limit-number-of-selections-in-multiple-select/184017/3 "2010-02-24T12:26:21Z")

</div>

On 24 February 2010 11:12, Andy J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> (well, you could do it with validation on the model)

Isn’t that where it would be best?

> You can do this:

but of course, we all know that relying solely on JS for validation is  
a sure method for letting duff data get into your DB (because not  
every browser has JS enabled or supported)

By all means, use the JS to enhance the front end; but if you have to  
ensure there’s no more than a certain amount of associations, you  
_have_ to check that in the model.  
You could also check the params array coming into the controller, and  
see what the size is there (unfortunately, I don’t have time right  
this second to try it out, but if you haven’t sorted it yourself  
later, re-post for help and I’ll have a play)

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [February 24, 2010, 1:30pm UTC](https://www.ruby-forum.com/t/limit-number-of-selections-in-multiple-select/184017/4 "2010-02-24T13:30:09Z")

</div>

> > (well, you could do it with validation on the model)
> 
> Isn’t that where it would be best?

I agree.

> By all means, use the JS to enhance the front end; but if you have to  
> ensure there’s no more than a certain amount of associations, you  
> _have_ to check that in the model.  
> You could also check the params array coming into the controller, and  
> see what the size is there (unfortunately, I don’t have time right  
> this second to try it out, but if you haven’t sorted it yourself  
> later, re-post for help and I’ll have a play)

Completely agree with this.

Cheers,

Andy

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [February 25, 2010, 1:04am UTC](https://www.ruby-forum.com/t/limit-number-of-selections-in-multiple-select/184017/5 "2010-02-25T01:04:17Z")

</div>

Thank you all so much. Validation at the model level is an excellent  
idea. I should have thought about that. I am still getting into the  
habit of thinking the rails way.
