Jump to content

form validation


gaya

Recommended Posts

Hi, Please any1 tell me how to validate the form. I'm having two buttons ques and ans in a form. By clicking question button the same form with some text fields will get open. And same for clicking the answer button the same form with some text fields will get open.Both forms have add & clear button. I'm having single form validate function.By clicking add it have to enter ques and ans in the database to corresponding tables. But by clicking add by setting all the fields empty using the script i've to get some alerts like the field is empty. But i didnt get any alerts it enters the database as emptyfield.please tell me how to write script for both the buttons with some conditions. Please tell me how to do this.

Link to comment
Share on other sites

ya sure. The following is my form and script

<form action="a.php" method="post" name="choose" onSubmit="return validate();"><table align="center" border="1" width="850" height="500" bgcolor="FFF5EE" ><tr><td><table align="center" bgcolor="">  <tr><td align="center" colspan="10"><img src="img/laugh.gif" alt="Smiley" /></td></tr>  <tr><td align="center" style="color:#FF0000; text-decoration:blink;" colspan="10" ><b><?php echo $qtype2;?></b></td></tr><tr><td align="center"><input type="submit" name="question" value="question" /><input type="submit" name="answer" value="answer" /></td></tr>  <tr><td colspan="10"> </td></tr><input type="hidden" name="board_txt" id="board_txt" value="<?php echo $board_name; ?>" /><input type="hidden" name="subject_txt" id="subject_txt" value="<?php echo $subject_name; ?>" /><input type="hidden" name="qtype" id="qtype" value="<?php echo $qtype2; ?>" /> <?$board_name=$_POST['board_txt'];$subject_name=$_POST['subject_txt'];$qtype2=$_POST['qtype'];//echo "$board_name";if($_POST['question']=="question"){?><tr><td align="center" style="color:#FF0000; text-decoration:blink;" colspan="10" ><b><?php echo $qtype2;?></b></td></tr>  <tr>   <td>   Question<font color="red">*</font>   </td>   <td>   :   </td>     <td><textarea name="ques1" id="ques1" cols="25" rows="5" style="resize:none;"></textarea>   </td>   <td>	<table>	<tr>	<td width="10">	<div style="background-image:url(img/mic1_30.jpg); width:30px; height:25px;" >	<input type="file" id="quest_wav1" name="quest_wav1"  style="opacity:0;-moz-opacity:0 ;filter:alpha(opacity: 0);" size="1"  />	</div>	</td>	</tr>	<tr>	<td width="10">	<div style="background-image:url(img/img1.jpeg); width:30px; height:30px;" >	<input type="file" id="quest_img1" name="quest_img1"  style="opacity:0;-moz-opacity:0 ;filter:alpha(opacity: 0);" size="1"   />	</div>	</td>	</tr>	</table>   </td>   <td> </td>     <tr>   <td>Page No<font color="red">*</font></td><td>:</td>   <td><input type="text" name="pno" id="pno"  size="16" onkeypress="return isNumberKey(event, this.value)"/></td>   <td> </td>   <td> </td>   <td>Chapter No<font color="red">*</font></td><td>:</td>   <td><input type="text" name="cno" id="cno" size="16"  onkeypress="return isNumberKey(event, this.value)"/></td>   <td> </td>   <td> </td>  </tr>  <tr>   <td>Board</td><td>:</td>   <td><input type="text" name="board_txt" id="board_txt" size="16" readonly="" value="<?php echo $board_name;?>" style="background-color:#FFFFFF;" /></td>   <td> </td>   <td> </td>   <td>Subject</td><td>:</td>   <td><input type="text" name="subject_txt" id="subject_txt" size="16" readonly="" value="<?php echo $subject_name;?>" style="background-color:#FFFFFF;" />     <input type="hidden" name="language" id="language" value="<?php echo $language_name; ?>" />   <input type="hidden" name="qtype" id="qtype" value="<?php echo $qtype2; ?>" />   </td>   <td> </td>   <td> </td>  </tr>  <tr>   <td>Short Type</td>   <td>:</td>   <td>   <select id="short_type" name="short_type" >   <option value="" selected="selected">----</option>   <option value="Problem" >Problem</option>   <option value="Deleted">Deleted</option>   <option value="Very Short">Very Short</option>   <option value="Long">Long</option>   <option value="Short">Short</option>   <option value="Punctuation">Punctuation</option>   <option value="Vocabulary">Vocabulary</option>   <option value="Cogentorder">Cogentorder</option>   </select>   </td>   <td> </td>   <td> </td>   <!--<td>Explanation</td><td>:</td>   <td><input type="text" name="exp" id="exp" size="16"  /></td>   <td> </td>   <td> </td>-->  </tr>  <tr><td colspan="10">  </td></tr>  <tr>	  <td><font color="#FF0000"><b>Note :</b></font></td>	 <td colspan="2" align="center"><font color="#FF0000">*</font> Mandatory Fields</td>	 <td colspan="3" align="left"><img src="img/mic1_30.jpg" alt="Upload Wave" />Upload WAVE</td>	<td colspan="4" align="left"><img src="img/img1.jpeg" alt="Upload Image" />Upload IMAGE</td>  </tr>  <tr>  <tr><td colspan="10">  </td></tr>   <td colspan="10" align="center">   <input type="submit" name="add" value="Add" />	   <input type="reset" name="clear" value="Clear"  />   </td>  </tr> </table></td></tr><!--</table></form>--><?include("config/config_on.php");}else if($_POST['answer']=="answer"){?><tr><td align="center" style="color:#FF0000; text-decoration:blink;" colspan="10" ><b><?php echo $qtype2;?></b></td></tr>  <tr>   <td>   Statement1<font color="#0000FF">*</font>   </td>   <td>   :   </td>     <td><textarea name="stmt1" id="stmt1" cols="25" rows="5" style="resize:none;"></textarea>   </td>   <td>	<table>	<tr>	<td width="10">	<div style="background-image:url(img/mic1_30.jpg); width:30px; height:25px;" >	<input type="file" id="stmt_wav1" name="stmt_wav1"  style="opacity:0;-moz-opacity:0 ;filter:alpha(opacity: 0);" size="1"  />	</div>	</td>	</tr>	<tr>	<td width="10">	<div style="background-image:url(img/img1.jpeg); width:30px; height:30px;" >	<input type="file" id="stmt_img1" name="stmt_img1"  style="opacity:0;-moz-opacity:0 ;filter:alpha(opacity: 0);" size="1"   />	</div>	</td>	</tr>	</table>   </td>   <td> </td>   <td>   Statement2<font color="#0000FF">*</font>   </td>   <td>   :   </td>   <td><textarea name="stmt2" id="stmt2" cols="25" rows="5" style="resize:none;"></textarea>   </td>   <td> </td>   <td>	<table>	<tr>	<td width="10">	<div style="background-image:url(img/mic1_30.jpg); width:30px; height:25px;" >	<input type="file" id="stmt_wav2" name="stmt_wav2"  style="opacity:0;-moz-opacity:0 ;filter:alpha(opacity: 0);" size="1"  />	</div>	</td>	</tr>	<tr>	<td width="10">	<div style="background-image:url(img/img1.jpeg); width:30px; height:30px;" >	<input type="file" id="stmt_img2" name="stmt_img2"  style="opacity:0;-moz-opacity:0 ;filter:alpha(opacity: 0); " size="1"   />	</div>	</td>	</tr>	</table>   </td>    </tr>  <tr>   <td>   Option1<font color="red">*</font>   </td>   <td>   :   </td>     <td>   <textarea name="opt1" id="opt1" cols="25" rows="3" style="resize:none;"></textarea>   </td>   <td>	<table>	<tr>	<td width="10">	<div style="background-image:url(img/mic1_30.jpg); width:30px; height:25px;" >	<input type="file" id="opt_wav1" name="opt_wav1"  style="opacity:0;-moz-opacity:0 ;filter:alpha(opacity: 0);" size="1"  />	</div>	</td>	</tr>	<tr>	<td width="10">	<div style="background-image:url(img/img1.jpeg); width:30px; height:30px;" >	<input type="file" id="opt_img1" name="opt_img1"  style="opacity:0;-moz-opacity:0 ;filter:alpha(opacity: 0);" size="1"   />	</div>	</td>	</tr>	</table>   </td>   <td> </td>   <td>   Option2<font color="blue">*</font>   </td>   <td>   :   </td>     <td><textarea name="opt2" id="opt2" cols="25" rows="3" style="resize:none;"></textarea>   </td>   <td> </td>   <td>	<table>	<tr>	<td width="10">	<div style="background-image:url(img/mic1_30.jpg); width:30px; height:25px;" >	<input type="file" id="opt_wav2" name="opt_wav2"  style="opacity:0;-moz-opacity:0 ;filter:alpha(opacity: 0);" size="1"  />	</div>	</td>	</tr>	<tr>	<td width="10">	<div style="background-image:url(img/img1.jpeg); width:30px; height:30px;" >	<input type="file" id="opt_img2" name="opt_img2"  style="opacity:0;-moz-opacity:0 ;filter:alpha(opacity: 0); " size="1"   />	</div>	</td>	</tr>	</table>   </td>    </tr>      <td>	<table>	<tr>	<td width="10">	<div style="background-image:url(img/mic1_30.jpg); width:30px; height:25px;" >	<input type="file" id="opt_wav4" name="opt_wav4"  style="opacity:0;-moz-opacity:0 ;filter:alpha(opacity: 0);" size="1"  />	</div>	</td>	</tr>	<tr>	<td width="10">	<div style="background-image:url(img/img1.jpeg); width:30px; height:30px;" >	<input type="file" id="opt_img4" name="opt_img4"  style="opacity:0;-moz-opacity:0 ;filter:alpha(opacity: 0); " size="1"   />	</div>	</td>	</tr>	</table>   </td>    </tr>    <tr><td colspan="10">  </td></tr>  <tr>	  <td><font color="#FF0000"><b>Note :</b></font></td>	 <td colspan="2" align="center"><font color="#FF0000">*</font> Mandatory Fields</td>	 <td colspan="3" align="left"><img src="img/mic1_30.jpg" alt="Upload Wave" />Upload WAVE</td>	<td colspan="4" align="left"><img src="img/img1.jpeg" alt="Upload Image" />Upload IMAGE</td>  </tr>  <tr>  <tr><td colspan="10">  </td></tr>   <td colspan="10" align="center">   <input type="submit" name="add" value="Add" style="background-color:#FFFF00; font:Arial, Helvetica, sans-serif; color:#FF0000"/>	   <input type="reset" name="clear" value="Clear" style="background-color:#FFFF00; font:Arial, Helvetica, sans-serif; color:#FF0000"  />   </td>  </tr> <!--</table></td></tr>--></table></form> function validate(){alert("hai");var quest1=document.getElementById('ques1').value;var board_txt=document.getElementById('board_txt').value;var subject_txt=document.getElementById('subject_txt').value;var cno=document.getElementById('cno').value;var pno=document.getElementById('pno').value;short_type=document.getElementById('short_type');short_type1=short_type.options[short_type.selectedIndex].value;quest1=trim(quest1);board_txt=trim(board_txt);subject_txt=trim(subject_txt);pno=trim(pno);cno=trim(cno);if(quest1==""){alert("Please enter Question");document.getElementById('ques1').focus();return false;}else if (board_txt==""){alert("Please Select Board");document.getElementById('board_txt').focus();return false;}else if (subject_txt==""){alert("Please Select Subject");document.getElementById('subject_txt').focus();return false;}else if( pno==""){alert("Please enter valid Page No");document.getElementById('pno').focus();return false;}else if (cno==""){alert("Please Select Chapter No");document.getElementById('cno').focus();return false;}}

Edited by gaya
Link to comment
Share on other sites

You can either write different validation functions for the if and the else, or have PHP write a variable into the Javascript code to tell it which set of fields to validate. I don't see the end of your else statement, but it doesn't look like you even write out a validation function if the question button was pressed.

Link to comment
Share on other sites

Yes i've not written for answer button.

function reply_click(clicked_id){alert(clicked_id);var clicked_id=this.id;}function validate(){/*var a=document.getElementById('1').value;alert(a);var b=document.getElementById('2').value;alert(;*/if(clicked_id=="ques"){alert("hai");var quest1=document.getElementById('ques1').value;var board_txt=document.getElementById('board_txt').value;var subject_txt=document.getElementById('subject_txt').value;var cno=document.getElementById('cno').value;var pno=document.getElementById('pno').value;short_type=document.getElementById('short_type');short_type1=short_type.options[short_type.selectedIndex].value;quest1=trim(quest1);board_txt=trim(board_txt);subject_txt=trim(subject_txt);pno=trim(pno);cno=trim(cno);if(quest1==""){alert("Please enter Question");document.getElementById('ques1').focus();return false;}else if (board_txt==""){alert("Please Select Board");document.getElementById('board_txt').focus();return false;}else if (subject_txt==""){alert("Please Select Subject");document.getElementById('subject_txt').focus();return false;}else if( pno==""){alert("Please enter valid Page No");document.getElementById('pno').focus();return false;}else if (cno==""){alert("Please Select Chapter No");document.getElementById('cno').focus();return false;}}else if(clicked_id=="ans"){alert("hhh");var stmt1=document.getElementById('stmt1').value;}}

But now its seems like when clicking answer button, it doesnt come to the elseif condition.I dnt knw wat i did wrong in that part. please tell me.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...