JavaScript Validation Needed for a Form

Discussion in 'JavaScript' started by cuashraf, Jun 15, 2012.

  1. #1
    I am not a professional programmer and try to do some light coding. I created the form below doing some research in the web but I can't prepare JavaScript validation scripts so that user can't keep any field blank. I didn't even declare any ID for the form. Please come up with something so that I can proceed.
    
    <form action="upload_results.php" enctype="multipart/form-data" method="post">
    <table align="center" bgcolor="#CCCCCC" width="250px">
    		<tr>
    			<td align="left"><b>Exam Name:</b></td>
    		</tr>
    		<tr>
    			<td align="left">
    				<select name="ex_id">
    				<?php
    				foreach($exam_ary as $key=>$value) {
    					echo "<option value='$key'>$value</option>";
    				}
    				?>
    				</select>
    			</td>
    		</tr>
    		<tr>
    			<td align="left"><b>Date of Exam:</b></td>
    		</tr>
    		<tr>
    			<td align="left">
    				<?php
    					  $myCalendar = new tc_calendar("date5", true, false);
    					  $myCalendar->setIcon("calendar/images/iconCalendar.gif");
    					  //$myCalendar->setDate(date('d'), date('m'), date('Y'));
    					  $myCalendar->setPath("calendar/");
    					  $myCalendar->setYearInterval(2000, 2015);
    					  $myCalendar->dateAllow('2008-05-13', '2014-12-31');
    					  $myCalendar->setDateFormat('j F Y');
    					  //$myCalendar->setHeight(350);
    					  //$myCalendar->autoSubmit(true, "form1");
    					  $myCalendar->setAlignment('left', 'bottom');
    					  $myCalendar->writeScript();
    					  ?>
    			</td>
    		</tr>
    		<tr>
    			<td align="left"><b>Result File:</b></td>
    		</tr>
    		<tr>
    			<td align="left">
    				<input type="file" name="result_file">
    			</td>
    		</tr>
    		<tr>
    			<td colspan="2" align="justify">
    				[You must upload either a <b>.xls (Microsoft Excel 2003)</b> or a <b>.pdf (Portable Document Format)</b> file]
    			</td>
    		</tr>
    		<tr>
    			<td colspan="2" align="center">
    				<input type="submit" name="add" value="Upload">
    			</td>
    		</tr>
    </table>
    </form>
    
    Code (markup):

     
    cuashraf, Jun 15, 2012 IP
  2. Ev0Lv

    Ev0Lv Greenhorn

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #2
    Use javascript. You can find the sample code over the net. Sorry i forgot the link where i got mine. :(
     
    Ev0Lv, Jun 17, 2012 IP
  3. Unni krishnan

    Unni krishnan Peon

    Messages:
    237
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    0
    #3
    You can try something like

    function checkEmptyInput()  {
        inputArray = document.getElementsByTagName("input");
        for (var index = 0; index < inputArray.length; index++) {
            if (inputArray[index].type = 'text') {
                if (inputArray[index].value == "") {
                        alert( "Please fill all the fields." );
                        inputArray[index].focus;
                        return false ;
                }
           }
        }
    }
    Code (markup):
    This will check if all the text fields are non-empty, otherwise throws an alert message.
     
    Last edited: Jun 27, 2012
    Unni krishnan, Jun 27, 2012 IP