0

Does anyone know how to go about validating the form that contains a radio button array(using JQuery)?

I have something like this...

6<input type="radio" name="answer[5]" value="6"> 
5<input type="radio" name="answer[5]" value="5"> 
4<input type="radio" name="answer[5]" value="4"> 

6<input type="radio" name="answer[13]" value="6"> 
5<input type="radio" name="answer[13]" value="5"> 
4<input type="radio" name="answer[13]" value="4"> 

The indicies are not necessarily in order. The questions are dynamic.

Is there a way to iterate through these answers in jquery??

2
  • 1
    How do you want to validate? Required validator? Commented May 7, 2014 at 20:20
  • Welcome to SO. Your question is too broad and will probably be put On Hold. Do some research into possible solutions, and come back with more specific questions (and your code). Commented May 7, 2014 at 20:27

3 Answers 3

1

Here's how I would do it:

function validate(){
    $('input [type=radio]').each(function(){
        if ($(this).val() !== 'something valid') return false;
    }
    return true;
}

if( !validate()) alert('invalid');
Sign up to request clarification or add additional context in comments.

Comments

0

How do you want to iterate through them? If you want to iterate through them all, you can just use the .each function such as:

$('input:radio[name=answer[]]').each(function(data){
   //Do something here
});

Comments

0

You could use jQuery to create subsets of radiobuttons accordingly

$('input [name="answer[5]"]);

This will gather all inputs that have name-attribute with value "answer[5]". After that you can iterate through that subset like this

$('input [name="answer[5]"])[i].val(); // or any other attribute you wish to get from query.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.