?
Solved

PHP preg_match and Date of Birth

Posted on 2014-01-30
8
Medium Priority
?
708 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
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

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.

Question has a verified solution.

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

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…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
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.
Suggested Courses

649 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