robert_gsfame Posted March 21, 2010 Share Posted March 21, 2010 again problem with checkbox...i am really newbie at javascript so hope that anyone could help i have this function function check() { if((document.getElementById('id1').checked==false)&&(document.getElementById('id2').checked==false)){ alert('please check at least one checkbox');}else{ alert('Are you sure?');} if there are 2 records with id=id1 and id=id2 then no problem will occur, what if let assume no record for id1 or in other words Null?? i have tried the above code but not working Thx for your help! } Link to comment https://forums.phpfreaks.com/topic/196009-validate-checkbox/ Share on other sites More sharing options...
robert_gsfame Posted March 21, 2010 Author Share Posted March 21, 2010 i also tried this but not working function check() { var a=0; for(i==1;i<3;i++){ if(document.getElementbyId(i)!==null){ if(document.getElementbyId(i).checked==false){ a++;}}} if(a==0){ alert('Please choose at least one');}else{ alert('thx for choosing');} } Link to comment https://forums.phpfreaks.com/topic/196009-validate-checkbox/#findComment-1029575 Share on other sites More sharing options...
robert_gsfame Posted March 21, 2010 Author Share Posted March 21, 2010 function check() { var a=0; for(i=1;i<3;i++){ if(document.getElementById(i)!==null){ if(document.getElementById(i).checked==true){ a++; } } } if(a>0){ alert('You have chosen !');}else{ alert('Please choose one');} } This is the correct one!! got it!! Link to comment https://forums.phpfreaks.com/topic/196009-validate-checkbox/#findComment-1029583 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.