Jump to content

[SOLVED] Easy "regular expression" question/solution...


ShootingBlanks

Recommended Posts

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';
  }
}

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.