Solved

PHP preg_match and Date of Birth

Posted on 2014-01-30
8
551 Views
Last Modified: 2014-02-01
I have a form that I am validating.  However I am having trouble with the Date Of Birth field.
I am using the following piece of code to validate the field but it is not 100% correct:
	// Check for a dob:
		if (preg_match ('/^[a-z A-Z 0-9 -\'.-]{2,20}$/i', $trimmed['dob'])) {
			$dob = $trimmed['dob'];
		} else {
			echo '<p class="error">Please enter a Date Of Birth!</p>';
		}

Open in new window

I need to validate the following input formats:
6/1/1999
6-1-1999
June 1, 1999

What do I need to add or take out of my "preg_match"???
0
Comment
Question by:rgranlund
8 Comments
 
LVL 34

Expert Comment

by:gr8gonzo
Comment Utility
First off, you should understand that 6-1-1999 is not the same as 6/1/1999. In almost every case, when you see dashes, the assumption is that you're using European formats, which are either DD-MM-YYYY or YYYY-MM-DD. So 6-1-1999 is typically understood as the January 6th, 1999 while 6/1/1999 is June 1st, 1999.

I would suggest using strtotime() to validate instead of a regular expression:

<?php
date_default_timezone_set("UTC");

$arr = array();
$arr[] = '6/1/1999';
$arr[] = '6-1-1999';
$arr[] = 'June 1, 1999';

foreach($arr as $val)
{
  $ts = strtotime($val);
  echo $val . " => " . $ts . " => " . date("c",$ts) . "\n";
}
?>
0
 
LVL 34

Expert Comment

by:gr8gonzo
Comment Utility
If you're just looking to get a true/false validation result:

function isValidDate($str)
{
  return (strtotime($str) > 0);
}

echo (isValidDate("6/1/1999") ? "Valid date!" : "Invalid date!");
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
You don't use regular expressions to do this.  You use strtotime() and date().  This article explains it, with examples.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
0
 
LVL 7

Author Comment

by:rgranlund
Comment Utility
I just need it to be true or false.  Is it valid or not.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
No, true or false is not good enough if you want to keep the information for any future use.  You need to normalize the date by converting it to the ISO-8601 standard data type.  It's all in the article, really!
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
If you are getting this thru an HTML form, I suggest you use dropdowns to select the correct info so you can format it yourself.  This eliminates dealing with the variable formats.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Thanks for the points and thanks for using EE! ~Ray
0
 
LVL 34

Expert Comment

by:gr8gonzo
Comment Utility
Umm... what part of my initial comment didn't answer your question?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now