1

I want to validate my form using jQuery. It has groups of radio buttons.

I have found multiple solutions for a single group of radio-buttons or solutions, where you have to specifically say what group-names there are.

Like here: Validate radio buttons with jquery?

The problem is, that the form is being generated at run-time. The questions are saved in a txt file (don't ask, it's a school exercise). The generated HTML-Code looks something like this:

<p>What&#039;s your gender? 

<input type="radio" name="gender" value="m" />Male 
<input type="radio" name="gender" value="f" />Female 

</p>

<p>

How satisfied are you with out support? 
<input type="radio" name="satisfaction" value="0%" />Not at all 
<input type="radio" name="satisfaction" value="25%" />Not very much 
<input type="radio" name="satisfaction" value="75%" />It was ok 
<input type="radio" name="satisfaction" value="100% />It was very satisfying

</p>

The js file is being generated using Twig, so it would be possible to loop through all the radio button groups, but I would like to avoid this if that's possible.

2
  • You can't get much better than iterating over each group Commented Nov 5, 2012 at 8:45
  • Can't you at least get all the group names directly in jquery, rather than generating tons of jquery code? So I can do the foreach directly in jQuery. Thanks for your effort Commented Nov 5, 2012 at 8:48

2 Answers 2

1

You would need to add all the groups first (page load would be fine, but make sure the group array is in global scope), then validate each group individually when your form is submitted/button click

var groups = [];
$(function() {
    $("input[type=radio][name]").each(function() {
      // Add the unique group name to the array
      groups[$(this).attr("name")] = true;
    });
});
$("button").click(function() {
    $.each(groups, function(i, o) {
      // For each group, check at least one is checked
      var isValid = $("input[name='"+i+"']:checked").length;
      alert(i + ": " + isValid);
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

groups is an empty array, when the form is submitted. But the function is getting executed. Any idea why? The Each-Block is also working.
0

I got it working like this

var groups = [];
$(function() {
    $("input[type=radio][name]").each(function() {
      // Add the unique group name to the array
      if (groups[groups.length - 1] != $(this).attr("name")) groups.push($(this).attr("name"));
    });
});
$('form').submit(function () {
    var isValid = true;

    $.each(groups, function(i, o) {
      // For each group, check at least one is checked
      if (isValid) isValid = $("input[name='"+o+"']:checked").length;  
    });

    if (!isValid) return false;

    return true;
});

Thanks to Blade0rz!

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.