0

My Php form (organisation.php) has this javascript validation

if(document.orgform.startdate.value == '')
{
    alert('Enter Start Date');
    document.orgform.startdate.focus();
    return false;
}
if(document.orgform.enddate.value == '')
{
    alert('Enter End Date');
    document.orgform.enddate.focus();
    return false;
} //these work fine

JQUERY

function getorgname(key)
{
  var orgvalue = key.value;
  $.ajax({
    type:"POST",
    url:"searchorg.php",
    data:{orgname : orgvalue},
    success: function(res) {
       $('#org').html(res);
    }
  });
} //returns organisation names with checkboxes, working fine

HTML Part of organisation part is as follows:

<input type="text" name="startdate" id="sdate" >
<input type="text" name="enddate" id="edate">
<div id="organisation></div> //ajax returned values displayed here, working fine.

Now how do I test if at least one checkbox is checked?

I tried putting validation in ajaxorganisation.php as well as orgnaisation.php and called that function while submitting organisation.php, but it does not work.

Server side validation is working fine, but would appreciate if there is a way for client side validation too

dr.dimitru
  • 2,645
  • 1
  • 27
  • 36
Siri
  • 1,344
  • 2
  • 10
  • 10
  • Where is CHECKBOXes in your html code? But in jQuery to get value use: `$(input).val()` – dr.dimitru Oct 21 '13 at 10:47
  • They come when I search for organisation.. ..Then I get Checkboxes and organisation names returned from the ajax file.. – Siri Oct 21 '13 at 10:53
  • The problem is I want to validate if at least one checkbox is checked and these checkboxes come from ajax file. I hope Im clear now – Siri Oct 21 '13 at 10:57
  • However, I found the above mentioned link suggested solutions only for a single php form that has checboxes, but no dynamical div generation. Anyways, thanks to all for the input..Got the perfect solution. Thanks to johan mårtensson – Siri Oct 21 '13 at 11:40

3 Answers3

1

A. Fix your html code. The containing div id attribute is missing the ending double quote delimiter.

B. whenever you wish to check if at least one is clicked, call the following function with an argument of '#organisation'

function at_least_one_checked(container) {
    var $c_list = $(container + ' input[type="checkbox"]');
    for (var i = 0; i < $c_list.length; ++i)
        if ($($c_list[i]).prop('checked'))
            return true;
    return false;
}
  • Wow..That did the miracle..The html fix was a typo..It is correct in my code. Thanks a lot – Siri Oct 21 '13 at 11:35
0

Better you should create a group of these checkboxes by keeping same name for them. Like in future if you've added more input element, it will not affect the code.

swapnil
  • 1
  • 3
  • but my question is how to validate if checkbox is checked when I submit the form...I should restrict the user to select at least one checkbox – Siri Oct 21 '13 at 11:03
0

try something like this

    // chkbx class on checkbpx element
    var flag = false
    jQuery('.chkbx').each(function(){
        if(this.checked){
            flag = true;
        }
    })

    return flag;// true if checked else false
rajesh kakawat
  • 10,826
  • 1
  • 21
  • 40