473,406 Members | 2,713 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,406 software developers and data experts.

How to validate checkbox???

Ajay Bhalala
119 64KB
Hi,

I have a registration form. I want to validate this form in php using javascript.

Here is my code of registration form :

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3.  
  4. <script type="text/javascript" language="javascript">
  5. function validate(frm)
  6. {
  7.     with(frm)
  8.     {
  9.         if(username.value=="")
  10.         {
  11.             alert("Please Enter Username!");
  12.             username.focus();
  13.             return false;
  14.         }
  15.         if(password.value=="")
  16.         {
  17.             alert("Please Enter Password!");
  18.             password.focus();
  19.             return false;
  20.         }
  21.         if(address.value=="")
  22.         {
  23.             alert("Please Enter Address!");
  24.             address.focus();
  25.             return false;
  26.         }
  27.         if(course.value=="" || course.value=="0")
  28.         {
  29.             alert("Please Select Course!");
  30.             course.focus();
  31.             return false;
  32.         }
  33.         if(hobby.value=="" || hobby.value=="0")
  34.         {
  35.             alert("Please Select Hobby!");
  36.             hobby.focus();
  37.             return false;
  38.         }
  39.         if(photo.value=="")
  40.         {
  41.             alert("Please Select Profile Picture!");
  42.             photo.focus();
  43.             return false;
  44.         }
  45.         if(gender.value=="")
  46.         {
  47.             alert("Please Select Gender!");
  48.             //gender.focus();
  49.             return false;
  50.         }
  51.         if(x.value=="" && y.value=="" && z.value=="" && p.value=="" && q.value=="")
  52.         {
  53.             alert("Please Select Skill!");
  54.             //x.focus();
  55.             return false;
  56.         }
  57.         return true;
  58.     }
  59. }
  60. </script>
  61. </head>
  62.  
  63.  
  64. <body>
  65. <form method="post" enctype="multipart/form-data" name="frmRegistration">
  66. <table>
  67.  
  68. <tr>
  69. <td>Enter the Username :-</td>
  70. <td>
  71. <input type="textbox" id="txtUN" name="username">
  72. </td>
  73. <tr>
  74.  
  75. <tr>
  76. <td>Enter the Password :-</td>
  77. <td>
  78. <input type="password" id="txtPassword" name="password">
  79. </td>
  80. </tr>
  81.  
  82. <tr>
  83. <td>Enter the Address :-</td>
  84. <td>
  85. <textarea id="txtPassword" name="address" rows="5" columns="10"></textarea>
  86. </td>
  87. </tr>
  88.  
  89. <tr>
  90. <td>Course :-</td>
  91. <td>
  92. <select name="course" id="course">
  93. <option value="0">Select Course</option>
  94. <option value="B.Sc.IT">B.Sc.IT</option>
  95. <option value="BCA">BCA</option>
  96. <option value="B.Com">B.Com</option>
  97. <option value="BBA">BBA</option>
  98. <option value="M.Sc.IT">M.Sc.IT</option>
  99. <option value="MCA">MCA</option>
  100. <option value="M.Com">M.Com</option>
  101. <option value="MBA">MBA</option>
  102. </select>
  103. </td>
  104. </tr>
  105.  
  106. <tr>
  107. <td>Hobby :-</td>
  108. <td>
  109. <select name="hobby" id="hobby" multiple="multiple">
  110. <option value="0" selected>Please Select</option>
  111. <option value="Reading">Reading</option>
  112. <option value="Writing">Writing</option>
  113. <option value="Collecting Different Things">Collecting Different Things</option>
  114. <option value="Travelling">Travelling</option>
  115. <option value="Walking">Walking</option>
  116. <option value="Playing">Playing</option>
  117. <option value="Cooking">Cooking</option>
  118. <option value="Music">Music</option>
  119. <option value="Other">Other</option>
  120. </select>
  121. </td>
  122. </tr>
  123.  
  124. <tr>
  125. <td>Profile Picture :-</td>
  126. <td>
  127. <input type="file" name="photo"/>
  128. </td>
  129. </tr>
  130.  
  131. <tr>
  132. <td>Gender :-</td>
  133. <td>
  134. <input type="radio" name="gender" value="Male">Male
  135. <input type="radio" name="gender" value="Female">Female
  136. </td>
  137. </tr>
  138.  
  139. <tr>
  140. <td>Skills :-</td>
  141. <td>
  142. <input type="checkbox" name="x" value="php">PHP
  143. <input type="checkbox" name="y" value="rdbms">RDBMS
  144. <input type="checkbox" name="z" value="java">Java
  145. <input type="checkbox" name="p" value="asp">ASP.NET
  146. <input type="checkbox" name="q" value="dcn">DCN
  147. </td>
  148. </tr>
  149.  
  150. <tr>
  151. <td colspan="2">
  152. <br>
  153. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  154. <input type="submit" name="submit" value="Submit" onclick="javascript:return validate(document.frmRegistration);">
  155. <input type="reset" name="clear" value="Reset">
  156. <br><br>
  157. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  158. <input type="submit" name="view" value="  View Records  ">
  159. </td>
  160. </tr>
  161.  
  162. </table>
  163. </form>
  164. </body>
  165. </html>
  166.  
There is no such type of physical error in this code.

When I Submit my form without entering any values, It displays proper messages for Username, Password, Address, Course, Hobby, ProfilePic, and gender.

But it doesn't display the message for checkboxes.


What should I do?????


Please help
Dec 29 '15 #1
4 2028
Dormilich
8,658 Expert Mod 8TB
I want to validate this form in php using javascript.
you can validate it on the server using PHP or in the browser using JavaScript.

But it doesn't display the message for checkboxes.
that’s because checkboxes (and radio buttons) never change their value. they only change their checked state.
Dec 30 '15 #2
Ajay Bhalala
119 64KB
Hello dormilich,
Thank you for your reply.

In short, I want that the user must have to select at-least one checkbox.

If user does not select any one checkbox, then I want to give the message 'Please Select Skill'.

And I want to do this using Javascript

How can this be done?????
Dec 30 '15 #3
Dormilich
8,658 Expert Mod 8TB
How can this be done?????
as I have previously said, by checking the checkboxes’ checked state.
Dec 30 '15 #4
Ajay Bhalala
119 64KB
Hi,

Thanks for your reply.

Its work. Thanks you for your help.
Jan 4 '16 #5

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

Similar topics

4
by: Steph | last post by:
Hello, Can someone tell me the script to use for having a change on the same page when using checkbox function ? For example, i would to check one condition and display dynamically a button...
2
by: Scott Natwick | last post by:
I would like to validate that a Checkbox is checked on the client-side. I have tried the "requiredfieldvalidator" and the "regularexpressionvalidator", but haven't been able to get either of them...
1
by: jayparaiso | last post by:
Hi! How to validate check box and drop down menu in one form?
0
by: ev45ive | last post by:
Hi! I have smth. like this: file.php: <?php SmartyValidate::connect($smarty); //SmartyValidate::register_form('register');
1
by: Scott | last post by:
I'm trying to validate a checkbox below on Sumbit. If the checkbox named "myForm.myCheckBox" isn't checked, then the code in CODE 1 displays False, which is correct. However, when I test that...
29
by: Amer Neely | last post by:
I've got a dynamically built form with checkboxes for each element ( a list of file names in a directory). I need to grab only those checkboxes that are checked, so I can then delete those files. ...
1
by: mbarnhizer | last post by:
Hello All, Trying to figure out how to validate a series of questions on an online test. I am thinking that VB or Javascript is the best route, but your input may make a difference. The site i...
0
by: TechnoAtif | last post by:
<?php include "dbconnect.php"; include "commonFunc.php"; ?> <!----------------------------------> <table width="80%" border="1" cellpadding="2" cellspacing="0"> <tr > <td...
0
by: Ned Balzer | last post by:
I posted this this morning but it never went through, so I am trying again -- apologies for the duplication if so. I need to validate several checkboxes on an asp.net 2.0 page. I don't need to...
5
by: lolodede | last post by:
i would like to make sure user at least one checkbox and if it select otherand need to make sure to write something in text box <?php $name = 0; $cusID=0; $hobbies =0 ; if (isset ($_POST)) {...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.