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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:11:02+00:00 2026-06-07T14:11:02+00:00

I am trying to validate some groups of checkboxes using Jq but I don’t

  • 0

I am trying to validate some groups of checkboxes using Jq but I don’t know where I am doing wrong.

I use the same code to validate other forms (but not checkboxes and is working fine.

form.php

<form id=frm_ch>
<td><input type="checkbox" name="g1" id="h1" value="1" /></td>
<td><input type="checkbox" name="g1" id="d1" value="2" /></td>
<td><input type="checkbox" name="g1" id="a1" value="3" /></td>

<td><input type="checkbox" name="g2" id="h2" value="1" /></td>
<td><input type="checkbox" name="g2" id="d2" value="2" /></td>
<td><input type="checkbox" name="g2" id="a2" value="3" /></td>

<td><input type="checkbox" name="g3" id="h3" value="1" /></td>
<td><input type="checkbox" name="g3" id="d3" value="2" /></td>
<td><input type="checkbox" name="g3" id="a3" value="3" /></td>

validate.js

//global vars
var frm_ch = $("#frm_ch");
var g1 = $("name=g1");
var g2 = $("name=g2");
var g3 = $("name=g3");
var cInfo = $("#cInfo");

frm_ch.submit(function(){
    if(valG1() & valG2() & valG3()){

var g1 = $("name=g1").attr('value');
var g2 = $("name=g2").attr('value');
var g3 = $("name=g3").attr('value');

//validate functions are all the same so I am posting just one:

function valG1(){
    //if it's NOT valid
    if($('input[name=g1]:checked').size() == 0){
        cInfo.text("Please check a Checkbox");
        cInfo.addClass("error");
        return false;
        }
        //if it's valid
        else{
        cInfo.text("");
        cInfo.removeClass("error");
        return true;
    }
}
  • 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-07T14:11:04+00:00Added an answer on June 7, 2026 at 2:11 pm

    You have syntax errors. You are missing two closing curly braces and closing parenthesis (e.g. add

    }})

    to the end of your code). Also your submit handler is not returning anything. Additionally, as a part of DRY (do not repeat yourself) principle, you don’t want to have three different functions valG1, valG2, valG3 that essentially do the same thing. This code works.

    var frm_ch = $("#frm_ch");
    var g1 = $("name=g1");
    var g2 = $("name=g2");
    var g3 = $("name=g3");
    var cInfo = $("#cInfo");
    
    function valG1() {
        //if it's NOT valid
        if ($('input[name=g1]:checked').size() == 0 || 
            $('input[name=g2]:checked').size() == 0 || 
            $('input[name=g3]:checked').size() == 0) {
            cInfo.text("Please check a Checkbox");
            cInfo.addClass("error");
            return false;
        }
        //if it's valid
        else {
            cInfo.text("");
            cInfo.removeClass("error");
            return true;
        }
    }
    
    frm_ch.submit(function() {
        if (valG1()) {
    
            var g1 = $("name=g1").attr('value');
            var g2 = $("name=g2").attr('value');
            var g3 = $("name=g3").attr('value');
            return true;
        }
        else
        {
            return false;
        }
    })
            ​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to validate email using javascript but for some reason it is
I am trying to validate some form fields but it always errors out on
I am trying to use jquery validation to validate some input values on the
im trying to validate my form, but for some reason the SSN field wont
I'm trying to put together some validation code. I am trying to validate against
I'm trying to validate some form data before adding it to a database however
I'm trying to validate some of the field in my form. and I want
I'm trying to validate a simple XML file against an XML schema using the
I am trying to validate an e-mail address using javascript. The problem is the
I am trying to validate two checkboxes. One of them must be checked in

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.