Kemik Posted September 15, 2007 Share Posted September 15, 2007 Hello all, I'm trying to create a regex which only allows dates in the following format: dd-mm-yyyy So I can insert it in to my database (DATETIME). if(!eregi("(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d", $str)) { $this->validation->set_message('datetime', 'The %s field must be in this format: dd-mm-yyyy'); return FALSE; } else { return TRUE; } If I try it, I get the error The datetime field must be in this format: dd-mm-yyyy. Can anyone see where I'm going wrong? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/69464-date-regex/ Share on other sites More sharing options...
effigy Posted September 15, 2007 Share Posted September 15, 2007 EREG does not understand \d, this is a PREG feature. Also, there's no need to use case-insensitive matching with digits and punctuation. Quote Link to comment https://forums.phpfreaks.com/topic/69464-date-regex/#findComment-349051 Share on other sites More sharing options...
jitesh Posted September 19, 2007 Share Posted September 19, 2007 <?php $pattern = "/((0[1-9])|(1[0-9])|(2[0-9])|(3[0-1]))\-((0[1-9])|(1[0-2]))\-[1-9][0-9][0-9][0-9]/"; $value = "19-09-2007"; if(preg_match($pattern,$value)){ echo "Valid"; }else{ echo "Invalid"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/69464-date-regex/#findComment-350959 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.