Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9129489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:45:32+00:00 2026-06-17T07:45:32+00:00

I realize this isn’t a new topic and that it has been asked before.

  • 0

I realize this isn’t a new topic and that it has been asked before. I have read through various answers and, although I am getting better at Javascript, am confused by most of the responses.

I am asking my users to give a rating, where they rate both a category and company. I want Javascript to validate that both sets of radio buttons have an answer marked, and if possible, give separate alerts depending on the one that isn’t marked.

My code (omitting the tables they are wrapped in)

<form action="blah.php" method="post" onsubmit="return validaterating()"
name="ratings">

<input type="radio" name="categoryrate" value="5">
<input type="radio" name="categoryrate" value="4">
<input type="radio" name="categoryrate" value="3">
<input type="radio" name="categoryrate" value="2">
<input type="radio" name="categoryrate" value="1">

<input type="radio" name="companyrate" value="5">
<input type="radio" name="companyrate" value="4">
<input type="radio" name="companyrate" value="3">
<input type="radio" name="companyrate" value="2">
<input type="radio" name="companyrate" value="1">

</form>

Javascript:

function validaterating() {

var rate1 = document.forms.ratings.categoryrate;
var rate2 = document.forms.ratings.companyrate;

function validaterating() {
for (var i=0; i < rate1.length; i++) {
if (rate1[i].checked)
return true;
}
alert("Please rate the category.");
return false;
}

Now, I know this works to validate the first rating as I have it and is the validation setup that I think makes the most sense to me. However, I can’t get the second question (rate2) to validate. I’ve tried another for loop, using the && operator, and it stops working when I try to get the second to validate. Using the code as I’m writing it, how is that possible?

Thanks for your time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:45:34+00:00Added an answer on June 17, 2026 at 7:45 am

    http://jsfiddle.net/R5M5Y/

    HTML

    <form onsubmit="return test();" action="blah.php" method="post" name="ratings">
    category:
    <input type="radio" name="categoryrate" value="5">
    <input type="radio" name="categoryrate" value="4">
    <input type="radio" name="categoryrate" value="3">
    <input type="radio" name="categoryrate" value="2">
    <input type="radio" name="categoryrate" value="1">
    company:
    <input type="radio" name="companyrate" value="5">
    <input type="radio" name="companyrate" value="4">
    <input type="radio" name="companyrate" value="3">
    <input type="radio" name="companyrate" value="2">
    <input type="radio" name="companyrate" value="1">
        <input type='submit' />
    </form>
    

    JavaScript

    function test(){
      var category = document.getElementsByName("categoryrate");
      var check1 = 0;
      for(i=0;i<category.length;i++){
        if(category[i].checked){
          check1++;
          break;
        }
      }
      var company = document.getElementsByName("companyrate");
      var check2 = 0;
      for(i=0;i<company.length;i++){
        if(company[i].checked){
          check2++;
          break;
        }
      }
      if(check1 && check2){
      }else{
        alert("you must select ratings for both company and category");
        return false;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I realize this has been asked hundreds of times, however, I can't seem to
I realize this question has been asked several times, but I wanted to frame
I realize this is a basic question but I have searched online, been to
I hope this isn't too vague, but I'm stuck on a problem that has
I'm new to Gearman, but I understand the general concepts. I realize that this
Let me begin by saying I realize this isn't the best idea from an
I realize this question is pretty basic, but I'm really stuck. I have a
This isn't a school assignment or anything, but I realize it's a mostly academic
Disclaimer: I realize this isn't really secure - it isn't meant to be a
So I realize this is an issue that seems to be somewhat contested. I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.