Validate checkboxes in MVC form

Posted by Frode Lillerud on Stack Overflow See other posts from Stack Overflow or by Frode Lillerud
Published on 2010-06-04T13:02:43Z Indexed on 2010/06/11 12:52 UTC
Read the original article Hit count: 651

Filed under:
|
|

Hi, I have a table in an ASP.NET MVC2 form. On each row in the table there is a checkbox. Underneath the table there is a submitbutton.

I want to make the button disabled when NONE of the checkboxes are selected.

<% using Html.BeginForm(....) { %>
<table>
<% foreach (var item in Model) { %>
    <tr> <td>
    <input type="checkbox" name="selectedContacts" />
    </td></tr>
<% } //End foreach %> 
</table>
<% } //End using %> 

<input type="submit" value="Create selection" name="CreateSelectionAction" />

The number of lines/checkboxes will vary from 1 to many.

How can I use MVC2/jQuery to require the user to selected minimum one checkbox before clicking Submit button?

Edit; Of the three answers below I couldn't get any of them to work. Nothing happens when clicking the checkboxes, and no Javascript errors are raised. Setting up a small bounty.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about asp.net-mvc