First you will need to write some Javascript or JQuery in order to count the number of selected checkboxes on the front end. I prefer Javascript if you don't have any other front-end code apart from this, there is no need to import the whole JQuery library.
That said, first thing you need to do is add the actionFunction between a form
tag:
<apex:actionFunction name="yourActionFunctionName" action="{!yourControllerMethodThatRedirects}" rerender="yourFormIdOrAnything" />
Then you'll need to add class
attribute for HTML or styleClass
for inputField
checkboxes.
If you have an apex:commandButton
you will need to change it to html input - <input type="button" value="Submit" onclick="validateCheckboxes();" />
The Javascript part - Inside your page add the following:
<script type="text/javascript">
/* Cross Browser getElementsByClassName */
Node.prototype.getElementsByClassName = function(cn)
{
for (var r = [], e = this.getElementsByTagName('*'), i = e.length; i--;)
{
if ((' ' + e[i].className + ' ').indexOf(' ' + cn + ' ') > -1)
{
r.push(e[i]);
}
}
return r;
}
/* Get the actual elements */
function validateCheckboxes()
{
var checkboxes = document.getElementsByClassName('YOURCLASSNAMEHERE');
var counter = 0;
for (var i = 0; i < checkboxes.length; i++)
{
if (checkboxes[i].checked)
{
counter++;
}
}
if (counter == 2)
{
yourApexFunctionName();
}
else
{
alert('Your message here when they have selected less or more than 2 checkboxes');
}
}
</script>
I haven't tested the code but you should be able to figure out some minor changes if required.