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!!!