Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Best way to validate a date?

Status
Not open for further replies.

Elliott3

Programmer
Sep 5, 2002
347
CA

I am wondering what the best way to validate a date is?

CES
 
I think sleipnir214 is right. Do all possible validations at the browser side, especially using "onsubmit" event handler and javascript coding, and leave those validations against databases at the server side.
For more details see
 
Note:

I believe what sleipnir214 is talking about is breaking up the date into three input elements, day, month, year. When you submit the form there is no ambiguous format since you assemble the elements server side.
Validation is still necessary sinve they still could select Feb. 31 etc.

JavaScript is a good idea but you can't depend on it. It's best to never trust any cleint side submitted data. Always inspect and validate what is submitted.
 
Hi thanks for the suggestions. What I ended doing is.

list($month, $day, $year) = explode("/",$myDate);
if (!checkdate($month, $day, $year))
{

}

Works great! If you have any questions or comments, let me know.

CES
 
Does your solution take into consideration:
1. user enters 2-29-2003 or 29.2.2003 etc.?
2. if Javasciprt does the checking, how does the PHP code validate?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top