Solved

PHP preg_match and Date of Birth

Posted on 2014-01-30
8
629 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

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 …
This article discusses how to implement server side field validation and display customized error messages to the client.
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 …

688 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