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
- <html>
- <head>
- <script type="text/javascript" language="javascript">
- function validate(frm)
- {
- with(frm)
- {
- if(username.value=="")
- {
- alert("Please Enter Username!");
- username.focus();
- return false;
- }
- if(password.value=="")
- {
- alert("Please Enter Password!");
- password.focus();
- return false;
- }
- if(address.value=="")
- {
- alert("Please Enter Address!");
- address.focus();
- return false;
- }
- if(course.value=="" || course.value=="0")
- {
- alert("Please Select Course!");
- course.focus();
- return false;
- }
- if(hobby.value=="" || hobby.value=="0")
- {
- alert("Please Select Hobby!");
- hobby.focus();
- return false;
- }
- if(photo.value=="")
- {
- alert("Please Select Profile Picture!");
- photo.focus();
- return false;
- }
- if(gender.value=="")
- {
- alert("Please Select Gender!");
- //gender.focus();
- return false;
- }
- if(x.value=="" && y.value=="" && z.value=="" && p.value=="" && q.value=="")
- {
- alert("Please Select Skill!");
- //x.focus();
- return false;
- }
- return true;
- }
- }
- </script>
- </head>
- <body>
- <form method="post" enctype="multipart/form-data" name="frmRegistration">
- <table>
- <tr>
- <td>Enter the Username :-</td>
- <td>
- <input type="textbox" id="txtUN" name="username">
- </td>
- <tr>
- <tr>
- <td>Enter the Password :-</td>
- <td>
- <input type="password" id="txtPassword" name="password">
- </td>
- </tr>
- <tr>
- <td>Enter the Address :-</td>
- <td>
- <textarea id="txtPassword" name="address" rows="5" columns="10"></textarea>
- </td>
- </tr>
- <tr>
- <td>Course :-</td>
- <td>
- <select name="course" id="course">
- <option value="0">Select Course</option>
- <option value="B.Sc.IT">B.Sc.IT</option>
- <option value="BCA">BCA</option>
- <option value="B.Com">B.Com</option>
- <option value="BBA">BBA</option>
- <option value="M.Sc.IT">M.Sc.IT</option>
- <option value="MCA">MCA</option>
- <option value="M.Com">M.Com</option>
- <option value="MBA">MBA</option>
- </select>
- </td>
- </tr>
- <tr>
- <td>Hobby :-</td>
- <td>
- <select name="hobby" id="hobby" multiple="multiple">
- <option value="0" selected>Please Select</option>
- <option value="Reading">Reading</option>
- <option value="Writing">Writing</option>
- <option value="Collecting Different Things">Collecting Different Things</option>
- <option value="Travelling">Travelling</option>
- <option value="Walking">Walking</option>
- <option value="Playing">Playing</option>
- <option value="Cooking">Cooking</option>
- <option value="Music">Music</option>
- <option value="Other">Other</option>
- </select>
- </td>
- </tr>
- <tr>
- <td>Profile Picture :-</td>
- <td>
- <input type="file" name="photo"/>
- </td>
- </tr>
- <tr>
- <td>Gender :-</td>
- <td>
- <input type="radio" name="gender" value="Male">Male
- <input type="radio" name="gender" value="Female">Female
- </td>
- </tr>
- <tr>
- <td>Skills :-</td>
- <td>
- <input type="checkbox" name="x" value="php">PHP
- <input type="checkbox" name="y" value="rdbms">RDBMS
- <input type="checkbox" name="z" value="java">Java
- <input type="checkbox" name="p" value="asp">ASP.NET
- <input type="checkbox" name="q" value="dcn">DCN
- </td>
- </tr>
- <tr>
- <td colspan="2">
- <br>
-
- <input type="submit" name="submit" value="Submit" onclick="javascript:return validate(document.frmRegistration);">
- <input type="reset" name="clear" value="Reset">
- <br><br>
-
- <input type="submit" name="view" value=" View Records ">
- </td>
- </tr>
- </table>
- </form>
- </body>
- </html>
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