dflow Posted June 14, 2011 Share Posted June 14, 2011 i want to allow only this this date format:dd-mm-yyyy $dateregex="\d\d[-]\d\d[-]\d\d\d\d"; if(preg_match($dateregex, $DepartureDate)){//query} else{exit();} is this the correct way to do it? Quote Link to comment https://forums.phpfreaks.com/topic/239366-specific-date-format-help/ Share on other sites More sharing options...
xyph Posted June 14, 2011 Share Posted June 14, 2011 Close. You need delimiters on your expression http://www.php.net/manual/en/regexp.reference.delimiters.php Also, you don't need a character class for your hyphens.. Simply \d\d-\d\d-\d\d\d\d Quote Link to comment https://forums.phpfreaks.com/topic/239366-specific-date-format-help/#findComment-1229713 Share on other sites More sharing options...
.josh Posted June 14, 2011 Share Posted June 14, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/239366-specific-date-format-help/#findComment-1229714 Share on other sites More sharing options...
requinix Posted June 14, 2011 Share Posted June 14, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/239366-specific-date-format-help/#findComment-1229718 Share on other sites More sharing options...
gizmola Posted June 14, 2011 Share Posted June 14, 2011 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}) Quote Link to comment https://forums.phpfreaks.com/topic/239366-specific-date-format-help/#findComment-1229729 Share on other sites More sharing options...
dflow Posted June 14, 2011 Author Share Posted June 14, 2011 many thanks Quote Link to comment https://forums.phpfreaks.com/topic/239366-specific-date-format-help/#findComment-1229755 Share on other sites More sharing options...
xyph Posted June 14, 2011 Share Posted June 14, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/239366-specific-date-format-help/#findComment-1229760 Share on other sites More sharing options...
dflow Posted June 14, 2011 Author Share Posted June 14, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/239366-specific-date-format-help/#findComment-1229763 Share on other sites More sharing options...
gizmola Posted June 14, 2011 Share Posted June 14, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/239366-specific-date-format-help/#findComment-1229775 Share on other sites More sharing options...
xyph Posted June 15, 2011 Share Posted June 15, 2011 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] ); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/239366-specific-date-format-help/#findComment-1229817 Share on other sites More sharing options...
gizmola Posted June 15, 2011 Share Posted June 15, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/239366-specific-date-format-help/#findComment-1229834 Share on other sites More sharing options...
xyph Posted June 15, 2011 Share Posted June 15, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/239366-specific-date-format-help/#findComment-1229908 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.