CORT0619 Posted November 15, 2011 Share Posted November 15, 2011 I have the following code below to validate a date that the user types in. But I am getting the following error: "Warning preg_match() [function.preg-match]: Unknown modifier '('" if(!empty($_POST['exp_date'])) { $pattern = '(^[0-9]{1,2})' // 1 or 2 digits .'([^0-9a-zA-Z])' // not alpha or numeric .'([0-9]{1,2})' // 1 or 2 digits .'([^0-9a-zA-Z])' // not alpha or numeric .'([0-9]{1,4}$)'; // 1 to 4 digits $exp_date = trim($_POST['exp_date']); if(preg_match($pattern, $exp_date)){ $pattern = '(^[0-9]{1,2})' // 1 or 2 digits .'([^0-9a-zA-Z])' // not alpha or numeric .'([a-zA-Z]{1,})' // 1 or more alpha .'([^0-9a-zA-Z])' // not alpha or numeric .'([0-9]{1,4}$)'; // 1 to 4 digits if (preg_match($pattern, $exp_date)){ $pattern = '(^[a-zA-Z]{1,})' // 1 or more alpha .'([^0-9a-zA-Z])' // not alpha or numeric .'([0-9]{1,2})' // 1 or 2 digits .'([^0-9a-zA-Z])' // not alpha or numeric .'([0-9]{1,4}$)'; // 1 to 4 digits if (preg_match($pattern, $exp_date)){ $pattern = '(^[a-zA-Z]{1,})' // 1 or more alpha .'([0-9]{2})' // 2 digits .'([0-9]{4}$)'; // 4 digits if (preg_match($pattern, $exp_date)){ $pattern = '(^[0-9]{4})' // 4 digits .'([^0-9a-zA-Z])' // not alpha or numeric .'([0-9]{1,2})' // 1 or 2 digits .'([^0-9a-zA-Z])' // not alpha or numeric .'([0-9]{1,2}$)'; // 1 to 2 digits if (preg_match($pattern, $exp_date)){ $pattern = '(^[0-9]{2})' // 2 digits .'([0-9]{2})' // 2 digits .'([0-9]{4}$)'; // 4 digits if (preg_match($pattern, $exp_date)){ $pattern = '(^[0-9]{4})' // 4 digits .'([^0-9a-zA-Z])' // not alpha or numeric .'([a-zA-Z]{1,})' // 1 or more alpha .'([^0-9a-zA-Z])' // not alpha or numeric .'([0-9]{1,2}$)'; // 1 to 2 digits } else { $errors[]= 'Please enter a valid expiration date.'; } } else { $errors[]= 'Please enter a valid expiration date.'; } } else { $errors[]= 'Please enter a valid expiration date.'; } } else { $errors[]= 'Please enter a valid expiration date.'; } } else { $errors[]= 'Please enter a valid expiration date.'; } } else { $errors[]= 'Please enter a valid expiration date.'; } } else { $errors[]= 'Please input an expiration date.'; } Quote Link to comment Share on other sites More sharing options...
xyph Posted November 15, 2011 Share Posted November 15, 2011 http://www.regular-expressions.info/php.html Read about 'preg', as 'ereg' has been depreciated. Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 16, 2011 Share Posted November 16, 2011 Wow, that is a lot of code just to validate a date. Plus, why would you duplicate the error message for every false validation? The way you have all those nested if() statemetns - nothing would ever pass!!! The first two validations check that the value starts with 1 or 2 digits, but then the third validation checks to see if the value starts with alpha characters! No value would pass both those conditions. PHP has a built in function [strtotime()] that can parse string representations into a timestamp. It is by no means foolproof, but it is pretty good. I'd suggest you use strtotime() on the value. If the function returns false, consider the date invalid, else use the generated timestamp to format the date in the format you need (most likely YYYY-MM-DD for the database). function isDate($dateStr) { $dateTS = strtotime($dateStr); if(!$dateTS) { return false; } return date('Y-m-d', $dateTS); } If you need validations that strtotime() does not support, then please post your specific requirements and I'm sure someone can generate the requisite regex patterns. Quote Link to comment Share on other sites More sharing options...
CORT0619 Posted November 16, 2011 Author Share Posted November 16, 2011 Well I would just like a preg_match expression that will throw an error if the user enters anything that doesn't look like a regular date. Does anyone know any regular expressions that can do that? the closest one I could find so far is the one I posted however that has been throwing error after error. Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 16, 2011 Share Posted November 16, 2011 Well I would just like a preg_match expression that will throw an error if the user enters anything that doesn't look like a regular date. Did you even try the snippet of code I posted? Besides, YOU need to define what a "regular date" looks like. The strtotime() function can "validate" a lot more strings as a date than you could do with regular expressions - well at least without having to create a ton of different regular expressions. As I said, if YOU want to define the specific requirements for what a valid date is then we can help you create the regex needed. Quote Link to comment Share on other sites More sharing options...
CORT0619 Posted November 16, 2011 Author Share Posted November 16, 2011 Yes I did try your code and I also modified it however when I used it, it didn't throw any errors and just automatically excepted what I typed. But I understand what your saying I guess I would have to limit the user to one thing. I would like the user to be able to type in a date like so: 01/01/2011 but anything besides that can throw an error. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted November 16, 2011 Share Posted November 16, 2011 Why not use a jQuery datepicker? Then in <noscript> tags, you can add three <select> fields (month, day, year) for those users who have JS disabled. That would help to constrain the user to the proper format. You can then validate the input is within acceptable parameters and feed the values to checkdate to make sure the date selected is valid. Quote Link to comment Share on other sites More sharing options...
CORT0619 Posted November 16, 2011 Author Share Posted November 16, 2011 I think your right Pikachu that would be the best way to constrain a date since there are so many ways it can be typed in. Thanks all! Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 16, 2011 Share Posted November 16, 2011 Yes I did try your code and I also modified it however when I used it, it didn't throw any errors and just automatically excepted what I typed. The function I provided won't throw any errors - that is YOUR responsibility. The function does exactly as I explained. If the date can't be parsed into a date using strtotime() it will return false. Otherwise it will return a data in a common format. I've made a couple of minor modifications: 1. implemented trim on the string 2. Added a simple regex to change a date in the format 2-5-2002 to 2/5/2002 because where there are numbers separated by dashes, strtotime() would interpret it as dd-mm-yyyy instead of mm-dd-yyyy. By changing it to slashes for that particular format it will be interpreted as US users would expect. 3. Added an additional parameter to optionally set the format of the output. Revised function and test code: function isDate($dateStr, $format='Y-m-d') { $dateStr = trim($dateStr); $dateStr = preg_replace("#^(\d{1,2})-(\d{1,2})-(\d{1,4})$#", "$1/$2/$3", $dateStr); $dateTS = strtotime($dateStr); if(!$dateTS) { return false; } return date($format, $dateTS); } //Begin Test code $testDates = array( '02-15-2005', '3/12/2010', 'Jan 15, 1998', '10-OCT-05', '01/01/2011', '2011-12-25', '15-15-15', 'December 23rd 1905', 'Octoburary 12, 2003', '10-9-12', '12-12'); foreach($testDates as $date) { $validDate = isDate($date); if(!$validDate) { echo "{$date} : Not Valid<br>\n"; } else { echo "{$date} : {$validDate}<br>\n"; } } Output of test code: User Input : Validation Output -------------------------------- 02-15-2005 : 2005-02-15 3/12/2010 : 2010-03-12 Jan 15, 1998 : 1998-01-15 10-OCT-05 : 2005-10-10 01/01/2011 : 2011-01-01 2011-12-25 : 2011-12-25 15-15-15 : Not Valid December 23rd 1905 : 1905-12-23 Octoburary 12, 2003 : Not Valid 10-9-12 : 2012-10-09 12-12 : Not Valid That's not to say you should use this, just that it will work if implemented properly (as is the case with any code). Quote Link to comment 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.