ShootingBlanks Posted November 16, 2007 Share Posted November 16, 2007 I'm sorry to bother people here with this, but I'm SOOOO bad with Regular Expressions. I just need a simple one to check if the data entered into a text field is in the date format YYYY-MM-DD. So, I'd need to check: ####-##-## If anyone could help me finish the code below, I'd really appreciate it!: if ($_POST) { // ERROR CHECKING if ($_POST['due_date'] DOES NOT MATCH THE YYYY-MM-DD FORMAT) { $error = 'Please make sure that your date is in the format YYYY-MM-DD'; } } Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted November 16, 2007 Share Posted November 16, 2007 like this if (!ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $_REQUEST['co_dates'])) echo "Please enter date in YYYY-MM-DD format"; Quote Link to comment Share on other sites More sharing options...
ShootingBlanks Posted November 16, 2007 Author Share Posted November 16, 2007 Perfect - THANKS!!! Quote Link to comment Share on other sites More sharing options...
effigy Posted November 16, 2007 Share Posted November 16, 2007 The parens are excessive and you also need the ^ and $ anchors. Quote Link to comment Share on other sites More sharing options...
ShootingBlanks Posted November 16, 2007 Author Share Posted November 16, 2007 The parens are excessive and you also need the ^ and $ anchors. What do you mean by that? How should that expression be fixed, because it seemed to work fine for me. Thanks! Quote Link to comment Share on other sites More sharing options...
obsidian Posted November 16, 2007 Share Posted November 16, 2007 The parens are excessive and you also need the ^ and $ anchors. What do you mean by that? How should that expression be fixed, because it seemed to work fine for me. Thanks! As it is, I could enter "hello 2007-02-01" and it would still match. Effigy is recommending this: ^\d{4}-\d{2}-\d{2}$ That way, you're not overloading your parser with unnecessary parenthetical phrases, and you have your match anchored. Quote Link to comment Share on other sites More sharing options...
effigy Posted November 16, 2007 Share Posted November 16, 2007 I would use... if (!preg_match('/\A\d{4}-\d{2}-\d{2}\z/', $var)) { ... } Quote Link to comment Share on other sites More sharing options...
ShootingBlanks Posted November 16, 2007 Author Share Posted November 16, 2007 Cool - makes sense. Thanks again!!! Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted November 16, 2007 Share Posted November 16, 2007 As it is, I could enter "hello 2007-02-01" and it would still match. Effigy is recommending this: Thanks to effigy and obsidian for pointing that. I hadn't checked that lol. Anyway thanks again... 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.