Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

php uk date regex not working

Hi experts

I am trying to validate uk date input in a pHP form  by using

case 'ukdate':
{
$bret= $this->test_datatype($input_value,"/^(0[1-9]|[12][0-9]|3[01])[- ^\/.](0[1-9]|1[012])[- ^\/.](19|20)\d\d$/");
if(false == $bret)
{
$default_error_message = sprintf(E_VAL_UKDATE_CHECK_FAILED,$variable_name);
}
break;
}

the test_datatype function is working with other validations, so I assume that the regex is not properly written. I found it by googling as I do not have knowledge of regex at all  :-)

basically does not matter what I type, the test_datatype function return always true in this case

I think the issue is with \/ but I cannot figure it out what to change.
The format I am looking to  validate is dd/mm/yyyy
 
0
brillox
Asked:
brillox
  • 3
  • 2
1 Solution
 
brutaldevCommented:
The slashes in the front and that back of that reg ex string look wrong. Take the slashes out and the date format you've specified is matched.

It should look like this "^(0[1-9]|[12][0-9]|3[01])[- ^\/.](0[1-9]|1[012])[- ^\/.](19|20)\d\d$"

To test it use an online tool like NRegex.
0
 
brilloxAuthor Commented:
then I get a warning

Warning: preg_match() [function.preg-match.html]: Unknown modifier '\'
0
 
brutaldevCommented:
Sorry, because you are using / in your regex you need to delimit by something else (# is a good choice). Here is a sample of the kind of code you can use:
 
<?php
$date = "01/05/1988";
$pattern = "#^(0[1-9]|[12][0-9]|3[01])[- ^\/.](0[1-9]|1[012])[- ^\/.](19|20)\d\d$#";

if (preg_match($pattern, $date))
{
  echo "Valid!";
}
else
{
  echo "Nope...";
}
?>

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Ray PaseurCommented:
To test for a date, you should use strtotime() instead of regex.  See the article here:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

It's really this easy (no regex debugging required):

if (!$timestamp = strtotime($date_time_input_string)) echo "$date_time_input_string IS A BOGUS DATE";
0
 
brutaldevCommented:
For more clarification, you only need to the # (or some other valid character not in your regex) when you are in PHP. If you want to test the regex with other tools you need to only take what's between them.
0
 
brilloxAuthor Commented:
Thanks brutaldev, it worked. I did not know that i could use any delimiter; I thought / was the only one

Hi Ray, I will take into account also your suggestion

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now