Jump to content

Recommended Posts

Also, if it's the only thing in $DepartureDate then you need to specify start and end of string, or else it will be able to match for example "blah12-12-1234"

 

$dateregex="~^[0-9]{2}-[0-9]{2}-[0-9]{4}$~";

 

Also another note..this doesn't validate whether it's a real month, day or year.  For example, it will validate 99-99-9999

Also another note..this doesn't validate whether it's a real month, day or year.  For example, it will validate 99-99-9999

And on that topic, add some parentheses like

/^(\d\d)-(\d\d)-(\d\d\d\d)$/

and give the captured numbers to checkdate.

I'd suggest something like this that enforces day/month ranges to be acceptable.  For example, this will accept 07-11-2011 but will not accept 32-13-2011 or other dates that are obviously wrong.  You could use the same technique as used for day and month ranges to constrain the years to something reasonable as well, but I don't know what your application expects.  It will still allow for non-existant dates, so you'll still require further validation, but it's better than allowing dates like 00-99-0000.

 

((0[1-9])|(1|2[0-9])|(3[01]))-((0[1-9])|(1[0-2]))-(\d{4})

Personally, I don't see the point in trying to validate with RegEx when further validation is needed anyways. You're going to cause backtracking, slowing down the function, and have to perform the same validation that you would with a simpler expression.

 

Instead, use the quick RegEx for basic validation and splitting, and use PHP to confirm that it's a valid date.

 

Again, just my opinion.

Personally, I don't see the point in trying to validate with RegEx when further validation is needed anyways. You're going to cause backtracking, slowing down the function, and have to perform the same validation that you would with a simpler expression.

 

Instead, use the quick RegEx for basic validation and splitting, and use PHP to confirm that it's a valid date.

 

Again, just my opinion.

 

can you show and example?

 

also i have been bombarded by a bot and the regex is doing the job

Personally, I don't see the point in trying to validate with RegEx when further validation is needed anyways. You're going to cause backtracking, slowing down the function, and have to perform the same validation that you would with a simpler expression.

 

Instead, use the quick RegEx for basic validation and splitting, and use PHP to confirm that it's a valid date.

 

Again, just my opinion.

 

The regex provided could be moved into javascript so you get some reasonable hygiene clientside. 

Moving the RegEx over to client side would be a great idea if you wanted a quick sanitize without the user actually having to submit information. It will still need to be re-checked on the server-side though, as anything performed by the client can be manipulated therefor not trusted.

 

Here's a simple function I'd use server-side

<?php

$date = '12-10-2004';

if( validDate($date) ) echo 'Date is valid';
else echo 'Date isn\'t valid';

function validDate( $str ) {
if ( !preg_match( '/^(\d{2})-(\d{2})-(\d{4})$/', $str, $m ) )
	return FALSE;
return checkdate( $m[2], $m[1], $m[3] );
}


?>

Yes, the date has to be checked regardless of the regex used, because no regex can validate a date against a calendar.  I do want to address whether or not the javascript is going to be used.  The amount of time required to check against the simpler "/d" only ones vs. one that catches dates with months and days that are out of range is going to be the same, so there's no reason not to use one that catches bad dates, however, there is a reasonable argument to be made that on the serverside there is absolutely no reason to use a regex at all, since we all agree that a regex alone can not do the job.

Yeah, I was thinking that too. Here's a function that doesn't use RegEx at all. Probably much faster.

 

<?php

$dates = array(
'09injection-10-1950', // nonsense will be stripped
'01-01-2005', // not valid, above minimum year
'8-3-1975', // valid, leading 0s will be added
'05-5-1850' // Not valid, in the function it checks if the date is before 1900
);

foreach( $dates as $date ) {
if( ($date_clean=validDate($date,'1986')) != FALSE ) echo 'Date is valid';
else echo 'Date isn\'t valid.';
echo ' - '.$date_clean.' ('.$date.')<br>';
}

function validDate( $str, $min_year ) {
$a = explode('-',$str);
foreach( $a as $k=>$v )
	$a[$k] = str_pad(
		intval(
			ltrim($v)
		)
	,2,'0',STR_PAD_LEFT);
return ( count($a) == 3 && $a[2] <= $min_year && $a[2] > 1900 && checkdate($a[1],$a[0],$a[2]) ) ?
	$a[0].'-'.$a[1].'-'.$a[2] : FALSE;
}


?>

 

Bored and can't sleep :P

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.