Solved

PHP preg_match and Date of Birth

Posted on 2014-01-30
8
605 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 110

Accepted Solution

by:
Ray Paseur earned 500 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
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 
LVL 7

Author Comment

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

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 83

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 110

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

739 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