Page 1 of 1

validating date field input by user!

Posted: Wed Mar 18, 2009 1:10 am
by rwahdan
hi all,

i need to validate the date input by user! for some reason if a user put rubbish input like letters and signs, the code will continue to generate all records instead of "0" records! so i need to validate the date field entered by user! here is the form code and your help is appreciated!
<form name='Generate Report' action='theresults.php' method='POST' onsubmit='return verifyMe();'>
<table align="center" class='table_form_1' id='table_form_1' cellspacing='0'>
<tr>
<td class='ftbl_row_1' ><LABEL for='types' ACCESSKEY='none' ><b style='color:red'>*</b>Chose Report Type
</td>
<td class='ftbl_row_1a' ><select name='types' id='types'>
<option value='Attendance'>Attendance</option>
<option value='Employee Records'>Employee Records</option>
<option value='Helipad'>Helipad</option>
<option value='Events'>Events</option>
<option value='Inventory Usage'>Inventory Usage</option>
<option value='Inventory Quantity'>Inventory Quantity</option>
</select>
</td>
</tr>
<tr>
<td class='ftbl_row_2' ><LABEL for='startdate' ACCESSKEY='none' ><b style='color:red'>*</b>Chose Start Date: - Formate (mm/dd/yyyy)
</td>
<td class='ftbl_row_2a' ><input type='text' name='startdate' id='startdate' size='23' value=''>
</td>
</tr>
<tr>
<td class='ftbl_row_1' ><LABEL for='enddate' ACCESSKEY='none' ><b style='color:red'>*</b>Chose End Date: - Formate (mm/dd/yyyy)
</td>
<td class='ftbl_row_1a' ><input type='text' name='enddate' id='enddate' size='23' value=''>
</td>
</tr>
<tr>
<td colspan='2' align='right'><input type='submit' name='submit' value='Submit'>&nbsp;<input type='reset' name='reset' value='Reset'>
</td>
</tr>
</table>
</form>
the html have the verify function:

<html>
<head>
<script language='javascript'>
function verifyMe(){
var msg='';

if(document.getElementById('types').value==''){
msg+='- Chose Report Type\n\n';}

if(document.getElementById('startdate').value==''){
msg+='- Chose Start Date:\n\n';}

if(document.getElementById('enddate').value==''){
msg+='- Chose End Date:\n\n';}

if(msg!=''){
alert('The following fields are empty or invalid:\n\n'+msg);
return false
}else{
return true }

}
</script>

Re: validating date field input by user!

Posted: Wed Mar 18, 2009 1:48 am
by susrisha
http://www.google.co.in/search?hl=en&rl ... arch&meta=

And its always advisable to use a datepicker(the calender icon with the dates displayed) for entering dates. This way its user friendly and also reduces chances of errors.

You might also want to google for datepicker + javascript.