Javascript checkbox validation

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Apr 2008
Posts: 511
Reputation: veledrom is an unknown quantity at this point 
Solved Threads: 0
veledrom veledrom is offline Offline
Posting Pro

Javascript checkbox validation

 
0
  #1
Dec 5th, 2008
Hi,

How do i check if any of these checkboxs are selected. If nothing is selected then invalidate the form. Javascript

thanks

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <input type="checkbox" name="MYcheckbox[]" value="0">
  2. <input type="checkbox" name="MYcheckbox[]" value="1">
  3. <input type="checkbox" name="MYcheckbox[]" value="2">
  4. <input type="checkbox" name="MYcheckbox[]" value="3">
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 91
Reputation: brechtjah is an unknown quantity at this point 
Solved Threads: 9
brechtjah's Avatar
brechtjah brechtjah is offline Offline
Junior Poster in Training

Re: Javascript checkbox validation

 
0
  #2
Dec 5th, 2008
You can iterate trough forum controls with Javascript
  1. var i;
  2. for(i=1;i<18;i++) {
  3. document.formPermissions.elements[i].checked = 1;
  4. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 511
Reputation: veledrom is an unknown quantity at this point 
Solved Threads: 0
veledrom veledrom is offline Offline
Posting Pro

Re: Javascript checkbox validation

 
0
  #3
Dec 5th, 2008
I couldn't do it. I generate those checkboxes by php and store values in them. name="MYcheckbox[]".
As i am a new person in jScript, i need help.

Thanks
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 91
Reputation: brechtjah is an unknown quantity at this point 
Solved Threads: 9
brechtjah's Avatar
brechtjah brechtjah is offline Offline
Junior Poster in Training

Re: Javascript checkbox validation

 
0
  #4
Dec 6th, 2008
What you need to do in PHP is give each checkbox an individual ID then,
as in:
  1. echo "<input type='checkbox' id='checkbox".$i."' name='".$checkboxName."'>&nbsp;</input>";

Now you can iterate trough them in javascript by doing this:
  1. var i;
  2. var isItChecked;
  3. for(i=0;i<9;i++) {
  4. isItChecked = document.myFormName.elements[i].checked;
  5.  
  6. }

Hope this helps ^^
brecht
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 511
Reputation: veledrom is an unknown quantity at this point 
Solved Threads: 0
veledrom veledrom is offline Offline
Posting Pro

Re: Javascript checkbox validation

 
0
  #5
Dec 6th, 2008
Ok i'll try that way. Thank you
Last edited by veledrom; Dec 6th, 2008 at 8:21 am. Reason: .
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 511
Reputation: veledrom is an unknown quantity at this point 
Solved Threads: 0
veledrom veledrom is offline Offline
Posting Pro

Re: Javascript checkbox validation

 
0
  #6
Dec 6th, 2008
Only when there are more than one DOM objects with the same ID, the DOM treats that as an array, it says in a web side. This is exactly what i try to do, but i couldn't find any.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 91
Reputation: brechtjah is an unknown quantity at this point 
Solved Threads: 9
brechtjah's Avatar
brechtjah brechtjah is offline Offline
Junior Poster in Training

Re: Javascript checkbox validation

 
0
  #7
Dec 6th, 2008
I should stop writing my posts so late, anyway
Looking back at my previous post I don't even think you HAVE to give them a unique ID to be able to iterate trough them. ID is there to give each element a unique identifier though, I cannot locate that site you talk of, maybe you can point me to it so I can read up on that. Anyway, if you could give me a piece of code I could try and help with that, see where that goes.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 511
Reputation: veledrom is an unknown quantity at this point 
Solved Threads: 0
veledrom veledrom is offline Offline
Posting Pro

Re: Javascript checkbox validation

 
0
  #8
Dec 8th, 2008
I want to add a javascript to check if any checkbox is selected or not.
Thanks

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <title>Untitled Document</title>
  4. </head>
  5.  
  6. <body>
  7. <form name="form1" method="post" action="save.php">
  8. <table>
  9. <tr>
  10. <td>ID</td>
  11. <td>COUNTRY</td>
  12. <td>Unit</td>
  13. <td>&nbsp;</td>
  14. </tr>
  15. <?php
  16. $connection=mysql_connect("localhost", "root", "");
  17. if ($connection) {
  18. mysql_select_db("phptest", $connection);
  19. } else {
  20. echo "Database connection was failed";
  21. exit();
  22. }
  23.  
  24. $sql="SELECT id, name FROM country_1";
  25. $runSql=mysql_query($sql);
  26.  
  27. if (@mysql_num_rows($runSql)>0) {
  28. while ($newArray=mysql_fetch_array($runSql)) {
  29. ?>
  30. <tr>
  31. <td><?php echo $newArray['id']; ?></td>
  32. <td><?php echo $newArray['name']; ?></td>
  33. <td><input type="textbox" name="textboxUnit[<?php echo $newArray['id']; ?>]" value=""></td>
  34. <td><input type="checkbox" name="checkboxCountry[<?php echo $newArray['id']; ?>]" value="<?php echo $newArray['id']; ?>"></td>
  35. </tr>
  36. <?php
  37. }
  38. }
  39. ?>
  40. </table>
  41. </form>
  42. </body>
  43. </html>
Last edited by veledrom; Dec 8th, 2008 at 1:20 pm. Reason: no code
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 91
Reputation: brechtjah is an unknown quantity at this point 
Solved Threads: 9
brechtjah's Avatar
brechtjah brechtjah is offline Offline
Junior Poster in Training

Re: Javascript checkbox validation

 
0
  #9
Dec 8th, 2008
  1. onclick="if(this.checked == 1) { alert('checked: '+<? echo $newArray['id'];?>); }"

Hopefully this is what you want : - )
Last edited by brechtjah; Dec 8th, 2008 at 5:01 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 511
Reputation: veledrom is an unknown quantity at this point 
Solved Threads: 0
veledrom veledrom is offline Offline
Posting Pro

Re: Javascript checkbox validation

 
0
  #10
Dec 9th, 2008
Sorry that is my mistake. I meant to put a submit button to validate checkboxes in a javascript fuction. If anything is checked then return true, otherwise return false.

Thanks
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 4962 | Replies: 19
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC