473,388 Members | 1,340 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

validate multiple checkboxes using javascript

Hello All,

I am trying to validate multiple checkboxes whose values are stored in an array using php. I have been trying from a really long time but nothing's working out.

Can anyone help?

Here's the code:

Expand|Select|Wrap|Line Numbers
  1. <form id="create_user" name="create_user" method="post" action="creating_database.php" onsubmit="return validate_create_user_form();">
  2. <?php
  3. $priviliges = array("SELECT","INSERT","UPDATE","DELETE","FILE","CREATE","ALTER","INDEX","DROP","CREATE TEMPORARY TABLES","CREATE VIEW","SHOW VIEW","CREATE ROUTINE","ALTER ROUTINE","EXECUTE","GRANT","SUPER","PROCESS",
  4. "RELOAD","SHUTDOWN","SHOW DATABASES","LOCK TABLES", "REFERENCES", "REPLICATION CLIENT", "REPLICATION SLAVE");
  5.  
  6. echo "<table border=0 cellpadding=0 cellspacing=0 align=center width='100%' border='1'>";
  7. $count = count($priviliges)."<br>";
  8. //echo $count;
  9.  
  10. for($i = 0; $i < $count; $i++ ) 
  11. {
  12.     if($i == 0)
  13.         echo "<tr>";
  14.  
  15.     echo "<td align=left><input type=checkbox name=priviliges[] 
  16. value='$priviliges[$i]'>$priviliges[$i]</td>";
  17.     echo "</tr>";
  18. }
  19.  
  20. echo "</table>";
  21. ?>
  22. </form>
  23.  
I would want to validate the priviliges[] array which consists of the above mentioned values. If no privilige is checked, i want to display an error message saying to check atleast one privilige.

Any help would be appreciated.

Thanks and Regards,
Dream2rule
Aug 22 '07 #1
6 15608
acoder
16,027 Expert Mod 8TB
Use
Expand|Select|Wrap|Line Numbers
  1. document.getElementsByName("priviliges[]");
to get all the checkboxes in an array. Then loop over the checkboxes and keep checking the checked property. If none are checked, alert the error message. I assume you would do this onsubmit.

PS. privilege is spelled incorrectly!
Aug 22 '07 #2
Tried doing it this way but it does not help. :(

Expand|Select|Wrap|Line Numbers
  1.  
  2. for ( var i=0; i < document.getElementsByName("priviliges[]"); i++) 
  3.     //priviliges is the name of the group of checkboxes
  4.     {
  5.             if (document.getElementsByName("priviliges[i]").checked) 
  6.             {
  7.                 var priviliges_checked[i] = document.getElementsByName("priviliges[i]").value;
  8.             }
  9.     }
  10.     if( (priviliges_checked[i] == null) || (priviliges_checked[i] == "") )
  11.     {
  12.         alert("Please check atleast 1 privilege!");
  13.         return false;
  14.     }
  15.  
  16.  
Aug 22 '07 #3
acoder
16,027 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. var privs = document.getElementsByName("priviliges[]");
will give you an array. To get the count, use:
Expand|Select|Wrap|Line Numbers
  1. privs.length
and to index an item in the array:
Expand|Select|Wrap|Line Numbers
  1. privs[i]
so your code could be:
Expand|Select|Wrap|Line Numbers
  1. var privs = document.getElementsByName("priviliges[]");
  2. var privschecked = false;
  3. for (var i = 0; i < privs.length; i++) {
  4.   if (privs[i].checked) {
  5.    privschecked = true;
  6.    break;
  7.   }
  8. }
  9. if (!privschecked) {
  10.   alert('blah');
  11.   return false;
  12. }
  13. return true;
Aug 22 '07 #4
thanks a lot.. it helped :)
Aug 22 '07 #5
acoder
16,027 Expert Mod 8TB
Glad it helped.
Aug 22 '07 #6
Hi,
your code is very useful for me .thanks alot...
Jan 23 '12 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: varois83 | last post by:
Hi Newbie here. I have been working on creating a guestbook for my site as practice and am learning a lot. Do you guys validate your forms first on the client with javascript and then on the...
8
by: Ralph Freshour | last post by:
I have multiple checkbox's created with an array name because I have many on the same web page - their names are like: frm_chk_delete frm_chk_delete frm_chk_delete frm_chk_delete etc. Here...
1
by: guitarsajee | last post by:
hi.. i want to know how to validate file tag using javascript... plz let me know soon as possible.. thanx...
1
by: menmysql | last post by:
hi to all i want to send multiple parameters using javascript. i am trying as follows <a href="./Admin.jsp?per=null?per1=null">
1
by: mishasoft | last post by:
Hi all, I have a HTML multiple select (ListBox) control: <select id="mySelect" multiple> <option>Apple</option> <option>Orange</option> <option>Pineapple</option> ...
5
by: ghjk | last post by:
I have a web site with several web pages. They are having different number of variables. eg: first page having 4 variables in the form(Name, Phone, ID, Gender) second page having 10 variables in the...
12
by: BabyLucifer666 | last post by:
Hello All, (using Access2000) I have a form with multiple unbound checkboxes. What I would like to do is have the user check whoever needs to take a specific training course. My database is...
1
by: moose hoss | last post by:
How would I validate this select box using JavaScript? <form action="pizza/data.php" method="post" name="form" onsubmit="javascript:return form_validation();"/> <select name="team"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.