?
Solved

PHP preg_match and Date of Birth

Posted on 2014-01-30
8
Medium Priority
?
656 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 39821440
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 35

Expert Comment

by:gr8gonzo
ID: 39821452
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 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 39821469
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 7

Author Comment

by:rgranlund
ID: 39821476
I just need it to be true or false.  Is it valid or not.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39821630
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 84

Expert Comment

by:Dave Baldwin
ID: 39821837
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 111

Expert Comment

by:Ray Paseur
ID: 39826872
Thanks for the points and thanks for using EE! ~Ray
0
 
LVL 35

Expert Comment

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

Featured Post

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…
Suggested Courses

762 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