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'; } } Link to comment https://forums.phpfreaks.com/topic/77626-solved-easy-regular-expression-questionsolution/ 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"; Link to comment https://forums.phpfreaks.com/topic/77626-solved-easy-regular-expression-questionsolution/#findComment-392920 Share on other sites More sharing options...
ShootingBlanks Posted November 16, 2007 Author Share Posted November 16, 2007 Perfect - THANKS!!! Link to comment https://forums.phpfreaks.com/topic/77626-solved-easy-regular-expression-questionsolution/#findComment-392925 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. Link to comment https://forums.phpfreaks.com/topic/77626-solved-easy-regular-expression-questionsolution/#findComment-392927 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! Link to comment https://forums.phpfreaks.com/topic/77626-solved-easy-regular-expression-questionsolution/#findComment-392937 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. Link to comment https://forums.phpfreaks.com/topic/77626-solved-easy-regular-expression-questionsolution/#findComment-392939 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)) { ... } Link to comment https://forums.phpfreaks.com/topic/77626-solved-easy-regular-expression-questionsolution/#findComment-392942 Share on other sites More sharing options...
ShootingBlanks Posted November 16, 2007 Author Share Posted November 16, 2007 Cool - makes sense. Thanks again!!! Link to comment https://forums.phpfreaks.com/topic/77626-solved-easy-regular-expression-questionsolution/#findComment-392951 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... Link to comment https://forums.phpfreaks.com/topic/77626-solved-easy-regular-expression-questionsolution/#findComment-392956 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.