|
Programming C, bash, Python, Perl, PHP, Java, you name it. |
|
Thread Tools | Display Modes |
|
|||
PHP regular expression help
Hey guys, writing a regex for PHP to validate date input to be put in an entry in MySQL.
Format must be YYYY-MM-DD HH:MM:SS I have this: Code:
$must_match = "^[0-9]{4}\-[0-9]{2}\-[0-9]{2}\ [0-9]{2}\:[0-9]{2}\:[0-9]{2}$"; if(preg_match($must_match, $dte) && $dte) { if(!$dte) { $query = "INSERT INTO $table VALUES ('', NOW(), 'ajgraves', '$post');"; } else { $query = "INSERT INTO $table VALUES ('', '$dte', 'ajgraves', '$post');"; } } else { die("ERROR: You entered an invalid date"); } mysql_query($query);
__________________
I just saved a bunch of money on my car insurance by fleeing the scene of the accident! |
|
|||
UPDATE: The code above was borked in that leaving the field blank would always fail. I at least fixed that:
Code:
$must_match = "^[0-9]{4}\-[0-9]{2}\-[0-9]{2}\ [0-9]{2}\:[0-9]{2}\:[0-9]{2}$"; if((preg_match($must_match, $dte) && $dte) || (!$dte)) { if(!$dte) { $query = "INSERT INTO $table VALUES ('', NOW(), 'ajgraves', '$post');"; } else { $query = "INSERT INTO $table VALUES ('', '$dte', 'ajgraves', '$post');"; } } else { die("ERROR: You entered an invalid date"); } mysql_query($query); I'm thinking it has to do with maybe the way I check for the space? "\ " perhaps isn't the best way. Maybe someone more versed in this can help me
__________________
I just saved a bunch of money on my car insurance by fleeing the scene of the accident! |
|
|||
DOH! I fixed it! I forgot two "/"'s. It should look like this:
Code:
$must_match = "/^[0-9]{4}\-[0-9]{2}\-[0-9]{2}\ [0-9]{2}\:[0-9]{2}\:[0-9]{2}$/";
__________________
I just saved a bunch of money on my car insurance by fleeing the scene of the accident! |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Regular expressions: renaming files with 'sed' | J65nko | Guides | 26 | 15th October 2010 08:03 PM |
perl expression syntax | qsecofr | Programming | 3 | 16th February 2009 12:56 PM |
Mount filesystem with a regular user | ivanatora | FreeBSD General | 15 | 30th July 2008 08:51 AM |
are you an former bsdforum regular? | ephemera | Off-Topic | 18 | 28th July 2008 12:57 PM |