0

Hi all... I've been teaching myself PHP and MySQL for the past few weeks so I'm pretty new at it. But this question has more to do with javascript (which I have been using for some time) than it does PHP.

I am letting the user enter a date into a date field in an HTML form in either of the following formats:

mm-dd-yyyy or mm/dd/yyyy

However in order to load it into MySQL it needs to be in the format yyyy-mm-dd. I have used the following javascript validation in order to put the user entered form into the correct format so that it can be loaded into the database:

function checkForm(form) {
	var regExDate = /^(\d{1,2})[\/-](\d{1,2})[\/-](\d{4})$/;
	var entryDate = form.entryDate.value;
	var mm = "";
	var dd = "";
	var yyyy = "";

	if(form.entryDate.value == "" || !regExDate.test(entryDate)){
		alert("Enter a valid Date!");
		return false;
	}else if(form.title.value == ""){
		alert("Enter a valid Title!");
		return false;
	}else if(form.information.value == ""){
		alert("Enter valid Information!");
		return false;
	}else {
		if(entryDate.substr(1,1) == "/" && entryDate.substr(4,1) == "/" || entryDate.substr(1,1) == "-" && entryDate.substr(4,1) == "-" ){
			mm = "0" + entryDate.substr(0,1);
			dd = entryDate.substr(2,2);
			yyyy = entryDate.substr(5,4);
		}else if(entryDate.substr(2,1) == "/" && entryDate.substr(5,1) == "/" || entryDate.substr(2,1) == "-" && entryDate.substr(5,1) == "-" ){
			mm = entryDate.substr(0,2);
			dd = entryDate.substr(3,2);
			yyyy = entryDate.substr(6,4);
		}else if(entryDate.substr(2,1) == "/" && entryDate.substr(4,1) == "/" || entryDate.substr(2,1) == "-" && entryDate.substr(4,1) == "-"){
			mm = entryDate.substr(0,2);
			dd = "0" + entryDate.substr(3,1);
			yyyy = substr(5,4);
		}else {
			mm = "0" + entryDate.substr(0,1);
			dd = "0" + entryDate.substr(2,1);
			yyyy = entryDate.substr(4,4);
		}
		
		entryDate = yyyy + "-" + mm + "-" + dd;
		form.entryDate.value = entryDate;
		
		return true;
	}		
}

80% of the time the data loads into the database fine, however every once in a while the data is not processed in time and therefore there is an error in loading it into the database. I have also tried coding this validation in PHP and had the same result. Does the javascript not have enough time to process before the PHP loads the data into the database? Please Help!!!

2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by cloud09
0

Critical validation should not rely on javascript as users can turn it off and the code is viewable by all users who visit the site. The validation should be server-side (PHP) as its is secure and can't be affected by users. Also what do you mean by it isn't processed fast enough? No matter how fast the script is, the users connection may be slow, so it can't be time based. The request to enter the data into the DB shouldn't be sent until its validation. Data entered into a DB should always be processed by PHP, and if you want by javascript.

EDIT: You should use selects to let users put in dates. Use a text input with a maxlength of 4 for the year, and then validate it by checking that its 4 digits and all numbers. If the range years is short, like from 1990 - 2010, then use a select, but if is long, like for birth dates (1920 - 2010) use a the text field. You could also use js or PHP loops to create the selects. Example:

<?php
echo "<select name=\"month\" id=\"month\">\r\n";
for($i=1;$i<32;$i++) {
    echo "<option value=\"" . $i . "\">" . $i . "</option>\r\n";}
echo "</select>\r\n<select name=\"day\" id=\"day\">\r\n";
for($i=1;$i<13;$i++) {
    echo "<option value=\"" . $i . "\">" . $i . "</option>\r\n";}
echo "</select>\r\n<select name=\"year\" id=\"year\">\r\n";
for($i=1990;$i<2011;$i++) {
    echo "<option value=\"" . $i . "\">" . $i . "</option>\r\n";}
echo "</select>\r\n";
?>

Edited by cloud09: n/a

0

thanks for your input but then what can explain why sometimes it works and sometimes it doesn't (and yes i am sure the data entered is a valid date).

0

Test the code in IE or FF if you have firebug and see if there are javascript errors. When is the code being executed? The form should not be submitted until after the js has been run. As I said before, you need to have it validated by PHP as well. JS is not something you can rely on.
In your php, the the MYSQL query should be written like this, or something similar:

if(!mysql_query($query)) {
    die("The server has experienced an error and the data could not be entered: " . mysql_error());}

This will allow you to determine if the error is coming from the mysql query.

Edited by cloud09: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.