Click to See Complete Forum and Search --> : [RESOLVED] Date will not validate


monarch_684
09-02-2008, 09:33 PM
The format of the date should be mm/dd/yyyy. Why won't this work.

function checkDateFormat($birth){
//match the format of the date
if (preg_match ("/^([0-9]{2})/([0-9]{2})/([0-9]{4})$/", $birth, $parts)){
//check weather the date is valid of not
if(checkdate($parts[2],$parts[1],$parts[3]))
return true;
else
return false;
}
else
return false;
}

if(checkDateFormat($birth) != true)
echo "<h3>Your birthday must be in the format MM/DD/YYYY</h3>";

felgall
09-02-2008, 09:55 PM
There is no $parts[3] - only $parts[0], $parts[1], and $parts[2]

monarch_684
09-02-2008, 10:15 PM
Thanks for that. I fill stupid now for that mistake, but for some reason I am still not able to validate the date format.

Sheldon
09-02-2008, 11:41 PM
try print_r'ing the $parts?

if (preg_match ("/^([0-9]{2})/([0-9]{2})/([0-9]{4})$/", $birth, $parts)){
echo("<pre>");
print_r($parts);
die("</pre>");

NogDog
09-02-2008, 11:59 PM
There is no $parts[3] - only $parts[0], $parts[1], and $parts[2]
Negatory: [0] will be the entire pattern, [1] the first sub-pattern, etc.; and there are 3 sub-patterns.

Anyway, the first problem you need to address is that if you use "/" as your regexp delimiter, then you need to escape any literal "/" characters within the regexp via the "\" character, or else use a different special character for the regexp delimiter.

PS: checkdate() wants the args in the order month, day, year, so if...The format of the date should be mm/dd/yyyy.......then the call to checkdate should be:

checkdate($parts[1], $parts[2], $parts[3])

monarch_684
09-03-2008, 01:20 PM
Thanks NogDog, that fixed this problem.