phily245 Posted November 18, 2011 Share Posted November 18, 2011 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 https://forums.phpfreaks.com/topic/251389-regex-for-date/ Share on other sites More sharing options...
xyph Posted November 18, 2011 Share Posted November 18, 2011 You're checking for forward slashes in the RegEx, not dashes. Link to comment https://forums.phpfreaks.com/topic/251389-regex-for-date/#findComment-1289346 Share on other sites More sharing options...
phily245 Posted November 18, 2011 Author Share Posted November 18, 2011 *FACEPALM* Link to comment https://forums.phpfreaks.com/topic/251389-regex-for-date/#findComment-1289359 Share on other sites More sharing options...
Pikachu2000 Posted November 18, 2011 Share Posted November 18, 2011 You don't want to allow the user to enter a date freehand anyhow. Use <select> fields as the <noscript> alternative. Otherwise, someone will enter April 12, 2011 as 12-04-2011. Link to comment https://forums.phpfreaks.com/topic/251389-regex-for-date/#findComment-1289428 Share on other sites More sharing options...
phily245 Posted November 21, 2011 Author Share Posted November 21, 2011 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 https://forums.phpfreaks.com/topic/251389-regex-for-date/#findComment-1289964 Share on other sites More sharing options...
joe92 Posted November 21, 2011 Share Posted November 21, 2011 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 https://forums.phpfreaks.com/topic/251389-regex-for-date/#findComment-1289969 Share on other sites More sharing options...
phily245 Posted November 21, 2011 Author Share Posted November 21, 2011 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 https://forums.phpfreaks.com/topic/251389-regex-for-date/#findComment-1289982 Share on other sites More sharing options...
Pikachu2000 Posted November 21, 2011 Share Posted November 21, 2011 Don't bother, it's an exercise in futility. Does the user intend for 03-08-2011 to be August 3 or march 8? There's no way to tell, and both will validate, which is why I suggested select fields. Link to comment https://forums.phpfreaks.com/topic/251389-regex-for-date/#findComment-1289988 Share on other sites More sharing options...
phily245 Posted November 21, 2011 Author Share Posted November 21, 2011 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 https://forums.phpfreaks.com/topic/251389-regex-for-date/#findComment-1289992 Share on other sites More sharing options...
joe92 Posted November 21, 2011 Share Posted November 21, 2011 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 https://forums.phpfreaks.com/topic/251389-regex-for-date/#findComment-1289995 Share on other sites More sharing options...
phily245 Posted November 21, 2011 Author Share Posted November 21, 2011 <?php echo $_POST["startDate"]; ?> echos 20-11-2011 (if the date selected is the 20th November 2011) Link to comment https://forums.phpfreaks.com/topic/251389-regex-for-date/#findComment-1289996 Share on other sites More sharing options...
joe92 Posted November 21, 2011 Share Posted November 21, 2011 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 https://forums.phpfreaks.com/topic/251389-regex-for-date/#findComment-1289999 Share on other sites More sharing options...
phily245 Posted November 21, 2011 Author Share Posted November 21, 2011 I had an extra i in $_POST["expiryDate"] when submitting the date. I just want to crawl away and hide in shame. Link to comment https://forums.phpfreaks.com/topic/251389-regex-for-date/#findComment-1290014 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.