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
  • 2
    why you use checkbox and putted same name!!! please use radio instead of checkbox or try change name to g1[] instead of g1 Commented Jul 13, 2012 at 5:54

3 Answers 3

1

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;
    }
})
        ​
Sign up to request clarification or add additional context in comments.

2 Comments

"you don't want to have three different functions" but each function checks for different group og check boxes (g1, g2, g3) 'if ($('input[name=g1]:checked')' checks only for g1 group
You can pass the name of the group as an argument to the function, i.e. valG(group)
1

Your missing some quotations in your form which are not required but it is considered good practice to always use them

    <form id=frm_ch> <-- You have
    <form id="frm_ch"> <-- should be

You are also using bitwise operators in your conditional statement here

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

You want to use the conditional operator && to check if all the values are true

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

4 Comments

I think it won't be a problem
@Mr Srinivas Thanks for pointing that out I wasn't aware that the quotations were optional
@DeckerWBrower <form id=frm_ch> is a typing mistake. I will check now the operator
@DeckerWBrower "You want to use the conditional operator && to check if all the values are true" even if for some reason the single & works for my form validation, YES I needed && to work with my checkboxes. ty
0

Assume that checkbox are in this format: g1[], g1[]. g1[], g2[], g2[], ...

You could try this:

var frm_ch = $("#frm_ch");

frm_ch.submit(function(){

    for(var i = 1; i <= 3; i++){
        var $ch = $.each($("input[name='g"+i+"[]']:checked"), function() {
               var $this = $(this);
               return $this;
        });
        if(!$ch.val()) alert('check checkbox group n: '+i);
    }

});

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.