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 8589489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:04:12+00:00 2026-06-11T23:04:12+00:00

So, in a form I have multiple choice and therefore an array of answers

  • 0

So, in a form I have multiple choice and therefore an array of answers that I need validate. My attempt at the code is this, but it just returns that ‘i’ (from the .each() line) is not defined:

function validTextAnswers(i, answer){
    var answer = $j(this) 
    // ^ may not be needed, but I was using it 
    // when I was not passing arguements
    if( (answer.val()=="") || (textAnswers.val()==null )){
        alert('Note: Please fill in all answers.'),
        answer.focus()
        return false;
    } 
    return true;
}

function validQuizCreate(){
    var course = $j('#course-selection .selected')
    var question = $j('textarea[name="question"]')
    var textAnswers = $j('div.answer:visible').children('input[type="text"]');

    if( (question.val()=="") || (question.val()==null) ){
        alert('You have to supply a question.'),
        question.focus()
        return false;
    }

if( !textAnswers.each(validTextAnswers(i, answer)) ){
    console.log('all seems good!')
    return false
}

I’ve played around with the code quite a bit just it’s just breaking jQuery when I do.
(Note: Assume $ for $j, it’s to avoid collisions with prototype on another section of the site.)

Solution:

// jQuery filter() can be used for this:
var validTextAnswers = textAnswers.filter(function(){
        return this.value != '';
    }).length == '4'; // four because the test is A though D.

// If all 4 multiple choice answers are not empty, it returns true.

// so to use it I can say:
    if(!validTextAnswers){
        alert('Please fill in all multiple choice options.')
        return false;
    }
  • 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-11T23:04:13+00:00Added an answer on June 11, 2026 at 11:04 pm

    each returns a jQuery object which is always a “truethy” value.

    You can use filter:

    var isValid = textAnswers.filter(function() {
        return this.value == "invalild"
    }).length > 0;
    
    if (!isValid) {
        alert('PROBLEM!!!');
    }​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to develop a form that spans multiple pages, but requires just one
i have a form that has a multiple select drop down. a user can
I have a form that enables a user to update multiple alert rules records
Let us say that I have a form to search by multiple tags in
I need to find a way to have the multiple choice options in a
I have a form with n multiple choice questions implemented as radio buttons (using
Making a simple program which will generate a multiple choice form. I have an
I have a django form that allows a user to select multiple options: CARDS
I don't know why, but I remembered that when you have a symfony form
So this is my form (checkbox that allows multiple selections) echoPlease select questions to

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.