Link to home
Start Free TrialLog in
Avatar of freshwaterwest
freshwaterwestFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Multiple Radio Groups - allow only unique on each row and only allow 5 choices max

I need to create a form with multiple rows of radio groups

the choice/values for each row/group is 1,2,3,4,5

I need to make sure only one of each value is selected (i.e. so the visitor can't choose the same value twice in different rows) and that only five rows max can be selected.

I was trying to do it by:
- function on change of radio
- get the values of all and create an array
- see if there are duplicates in the array and alert to inform that different values must be chosen
- count the choices and alert if more than five.

here's the table test code I've been playing around with:
http://jsfiddle.net/3uxZY/26/

many thanks
Avatar of Gary
Gary
Flag of Ireland image

So basically all you need is to make sure that only 5 choices are made where they do not select 'Not Chosen' and that there is 1 of each value 1 through 5
Obvious answer would be sum the values and if it is > 15 then its not valid
Avatar of freshwaterwest

ASKER

I did think that myself at first, but then that would allow duplicates to be selected - I was hoping I might be able to check for duplicates as they were selected
Your first reply is correct as to what I'm trying to achieve. If I could alert when duplicate is selected that would be good.
So would something like this work
http://jsfiddle.net/GaryC123/3uxZY/29/

(I amended your radio to set a value of 0 for no choice, and fixed the double 4's)

$(document).ready(function () {
    $("#t").click(function(e){
        score=0

         $("[type=radio]:checked").each(function(){
        score+=parseInt($(this).val());
         })
         if(score!=0 &&
            score!=1 &&
            score!=3 &&
            score!=6 &&
            score!=10 &&
            score!=15){
      alert("Please only choose 5 from 1 to 5")
      e.preventDefault()
         }
        else{
            e.preventDefault()
            alert("It's Ok")
        }
    })
});

Open in new window


I wouldn't check for duplicates as they click, people make changes as they go and alerting them to fact or disabling the choice would be annoying
That's a big improvement but you can still choose: 2, 2, 5, 5, 1 for example (totals 15) but I really need to make sure there aren't any duplicate numbers (so we can get their first, second, third, fourth and fifth choices)

many thanks
good point about alerting as they click - on submit is better

thanks
This is a bit messy and can probably be cleaned up
http://jsfiddle.net/GaryC123/3uxZY/31/
That's brilliant - thanks for that - I could just do with checking that 5 have been checked or that they add up to 15.
Would it be possible to incorporate a count number of checks into this maybe (or just get the values and add to equal 15 as before)

cheers
Not sure I understand you.
Only 5 (or less) can be selected else you get an error
You cannot select the same value twice.
I think you can select for example: 1,2,3,4

(without a 5th) - otherwise it's perfect..

thanks
I thought you allowed less than 5 to be selected?
ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfect - many thanks - I've learnt a few things there : )