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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:28:15+00:00 2026-05-15T12:28:15+00:00

I am using the following javascript for form validation: <script type=text/javascript> function validate_form (

  • 0

I am using the following javascript for form validation:

<script type="text/javascript">
function validate_form ( )
{
    valid = true;

        if ( document.contact_form.contact_name.value == "" )
        {
                alert ( "Please fill in the 'Your Name' box." );
                valid = false;
        }

        if ( ( document.contact_form.gender[0].checked == false ) && ( document.contact_form.gender[1].checked == false ) )
        {
                alert ( "Please choose your Gender: Male or Female" );
                valid = false;
        }

        if ( document.contact_form.age.selectedIndex == 0 )
        {
                alert ( "Please select your Age." );
                valid = false;
        }

        if ( document.contact_form.terms.checked == false )
        {
                alert ( "Please check the Terms & Conditions box." );
                valid = false;
        }

        return valid;
}
</script>

The form:

<form name="contact_form" method="post" action="somepage.php" onSubmit="return validate_form();">

<h1>Please Enter Your Details Below</h1>

<p>Your Name: <input type="text" name="contact_name"></p>

<p>Your Gender: <input type="radio" name="gender" value="Male"> Male
&nbsp; <input type="radio" name="gender" value="Female"> Female</p>

<p>Your Age:

<select name="age">
    <option value="">Please Select an Option:</option>
    <option value="0-18 years">0-18 years</option>
    <option value="18-30 years">18-30 years</option>
    <option value="30-45 years">30-45 years</option>
    <option value="45-60 years">45-60 years</option>
    <option value="60+ years">60+ years</option>
</select>

<p>Do you agree to the Terms and Conditions?
<input type="checkbox" name="terms" value="Yes"> Yes

<p><input type="submit" name="send" value="Send Details"></p>

</form>

The validation works fine, except it displays one alert box after another to point out which fields haven’t been filled in or selected.

How can this script be modified so that all the missing fields are pointed out in ONE alert box like:

Please fill in the following fields:
Name, Gender, Age, Terms and
Conditions

  • 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-05-15T12:28:16+00:00Added an answer on May 15, 2026 at 12:28 pm

    Use a string or an array (as in the example below) to accumulate the error fields then generate the alert statement and return value based on its contents. For example:

    function validate_form () {
      var invalid=[], form=document.contact_form;
      if (form.contact_name.value == "") {
        invalid.push('Name');
      }
      if ((form.gender[0].checked == false) && (form.gender[1].checked == false)) {
        invalid.push('Gender');
      }
      if (form.age.selectedIndex == 0) {
        invalid.push('Age');
      }
      if (form.terms.checked == false) {
        invalid.push('Terms and Conditions');
      }
      if (invalid.length > 0) {
        alert('Please fill in the following fields: ' + invalid.join(', ') + '.');
        return false;
      }
      return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 494k
  • Answers 494k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer All css links all master page resolved relative to the… May 16, 2026 at 10:58 am
  • Editorial Team
    Editorial Team added an answer I had a look at the Advanced Rails Recipes book… May 16, 2026 at 10:58 am
  • Editorial Team
    Editorial Team added an answer auth_handler is only for basic HTTP authentication. The site here… May 16, 2026 at 10:58 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have the following basic function: <script type=text/javascript> function Form_Data(theForm) { var t=1; while
I'm using jquery.validate plugin and facing the following situation: <form id=myForm ....> <input type=text
Im using the bog standard form validation plugin. I have two radio buttons 'yes'
I'm currently creating a very small form on my homepage using HTML and JavaScript.
I'm using the following code (via ' Dark Side of the Carton ') to
I am using the following partial to render the editor for the Create and
I am using jquery-1.4.2 library and jquery.validate.js plugin to validate a very simple form.
I am using the following code to open a popup window and passing the
I send data from javascript to PHP like this: $.ajax({'url': 'my.php', 'type': 'POST', 'data':
I want as well as Client Side Validation as Server Side Validation. I realized

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.