Validating a date input in form

Hi I’ve searched the internet for how to validate date inputs on a form that updates my records. There is plenty of information, but I am having trouble putting it all together. I have read a lot of posts about regular expressions, the preg_match() function and the date() function. I wrote the following function to validate the date, but it doesn’t work when I key in a date with the wrong format.

function check_date ($approval_date) {
if(preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $approval_date)) {
    echo ''.$approval_date.'';
}else{
    echo 'Please enter date in correct format.';
}

}

This is the code for the input:

echo '<tr>

<td class="heading">Initial Registration Date: </td>

<td>

<span class="dates"><input type="text" name="approval_date" size="25" maxlength="25" value="' . date("m/d/Y", check_date($approval_date)) . '" /></span>

<span id="date_format"> (mm/dd/yyyy)</span>

</td></tr>';

I placed the function directly above the code for the input.

As you can see from the input code, I am populating the input from the database and using the date function to put it in a mm/dd/yyyy format.

I want the user to be able update the date in the database with this form.

If someone has a chance to take a look, I would be grateful.

Bill

For starters, you’re specifying a mm/dd/yyyy format but your regex starts off with a 4 digit number.