freshwaterwest
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
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
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
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
ASKER
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)
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
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")
}
})
});
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
ASKER
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
many thanks
ASKER
good point about alerting as they click - on submit is better
thanks
thanks
This is a bit messy and can probably be cleaned up
http://jsfiddle.net/GaryC123/3uxZY/31/
http://jsfiddle.net/GaryC123/3uxZY/31/
ASKER
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
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.
Only 5 (or less) can be selected else you get an error
You cannot select the same value twice.
ASKER
I think you can select for example: 1,2,3,4
(without a 5th) - otherwise it's perfect..
thanks
(without a 5th) - otherwise it's perfect..
thanks
I thought you allowed less than 5 to be selected?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Perfect - many thanks - I've learnt a few things there : )