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

 

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.