chiprivers Posted August 24, 2007 Share Posted August 24, 2007 I not so good at javascript cos I dont use it very often so some help with this one would be much appreciated. In a form I have two fields, one date and one time. When the form is submitted, I want to check that the date is submitted in the format 'dd/mm/yyyy' and that time is submitted as 'hhmm' (four digit number between 0000 and 2359). If either of these fields does not contain a valid entry then the form should not submit. Link to comment https://forums.phpfreaks.com/topic/66587-simple-form-validation/ Share on other sites More sharing options...
xenophobia Posted August 25, 2007 Share Posted August 25, 2007 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script language="javascript" type="text/javascript"> function valDate(dateinput) { var splitedDate = dateinput.split("/"); if(splitedDate.length != 3) { return false; }else{ var the_day = splitedDate[0]; var the_month = splitedDate[1]; var the_year = splitedDate[2]; if(the_day > 31 || the_day < 1) { return false; }else if(the_month > 12 || the_month < 1){ return false; }else if(the_year < 1){ return false; } return true; } } function valTime(timeinput) { if(timeinput.length != 4) return false; var thehour = timeinput.substring(0,2); var theminute = timeinput.substring(2,4); if(thehour > 23 || thehour < 0) return false; if(theminute > 59 || theminute < 0) return false; return true; } function validate() { var the_date = document.getElementById('txt_date').value; var the_time = document.getElementById('txt_time').value; if(valDate(the_date) && valTime(the_time)) { alert("Ok!"); }else{ alert("Wrong date!"); } } </script> </head> <body> <input type="text" id="txt_date" size="15" /> <input type="text" id="txt_time" size="15" /> <input type="button" value="Click Me" onClick="validate()" /> </body> </html> Copy paste the code above and try out. Hope it works! Link to comment https://forums.phpfreaks.com/topic/66587-simple-form-validation/#findComment-333748 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.