Jump to content

Regex for date


phily245

Recommended Posts

Hi,

I'm using the JQuery UI datepicker to insert a date into a form field, but I need validation in case the user enters the date by hand. The date needs to be dd-mm-yyyy. I currently have this:

<?php
if(!preg_match('/^\d{2}\/\d{2}\/\d{4}$/', $expiryDate)) {
$issue = "9";
return $issue;
}
if(!preg_match('/^\d{2}\/\d{2}\/\d{4}$/', $startDate)) {
$issue = "9";
return $issue;
}
?>

But it's not working and I'm pretty sure its the regex.

Link to comment
Share on other sites

Thanks for the advice. I changed my regex to the following code, but it still wouldn't work.

 

<?php
        if(!preg_match('/^\d{2}-\d{2}-\d{4}$/', $expiryDate)) {
	$issue = "9";
	return $issue;
}
        if(!preg_match('/^\d{2}-\d{2}-\d{4}$/', $startDate)) {
	$issue = "9";
	return $issue;
}?>

Link to comment
Share on other sites

Otherwise, someone will enter April 12, 2011 as 12-04-2011.

...that's the desired format ;)

 

Place the digit character set in character classes if you are looking for repetition;

<?php
        if(!preg_match('/^[\d]{2}-[\d]{2}-[\d]{4}$/', $expiryDate)) {
	$issue = "9";
	return $issue;
}
        if(!preg_match('/^[\d]{2}-[\d]{2}-[\d]{4}$/', $startDate)) {
	$issue = "9";
	return $issue;
}
?>

 

Also, thats two if's returning the same result that could be executed on the same line;

<?php
        if(!preg_match('/^[\d]{2}-[\d]{2}-[\d]{4}$/', $expiryDate) or !preg_match('/^[\d]{2}-[\d]{2}-[\d]{4}$/', $startDate)) {
	$issue = "9";
	return $issue;
}
?>

 

Furthermore, you might want to allow for people entering the date as dd/mm/yyyy or dd.mm.yyyy so you might want to include another character set for the separation sections instead of just a dash, e.g. [./\-].

 

Hope this helps,

Joe

Link to comment
Share on other sites

It's still not working, gah!

I tried what you suggested, then I tried using another one I found on google and slightly modified:

<?php
if(!preg_match('/^(19|20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])$/', $expiryDate) or !preg_match('/^(19|20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])$/', $startDate)) {
	$issue = "9";
	return $issue;
}
?>

 

I need the - seperator so that I can use explode() on the date function. When I get this working with just a -, I'm going to add support for [- \/\.] and a switch statement for the explode.

Link to comment
Share on other sites

It's meant to validate to 3rd August. We expect the user to enter it this way, as the end users are from the UK (like me) and we use dd-mm-yyyy. I also have an instruction to do it this way in the label on the field which the user enters the date into. I think a select statement will be overkill, as the dates needed can be from tomorrow to in multiple years time.

Link to comment
Share on other sites

How is the date getting posted to the validation script? Can you give an example of how it looks just before it hits the preg_match.. i.e. after any html_entities etc. It may be a case of the anchors around your pattern messing things up for you.

 

If it helps, I have this uk date validation working on my site:

preg_match("~[0-9]{1,2}/[0-9]{1,2}/[0-9]{1,4}~", $startDate)

Link to comment
Share on other sites

It works for me. I just ran the following code:

<?php

$testing = '20-11-2011';

echo $testing.'<br/><br/>';

if(!preg_match('/^[\d]{2}-[\d]{2}-[\d]{4}$/', $testing)){
echo 'no match';
}
else{
echo 'match';
}
?>

 

And it displayed:

20-11-2011

match

 

Indicating that the regex works fine. The problem must lie elsewhere.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.