0

Possible Duplicate:
How to check if a string is a legal “dd/mm/yyyy” date?

I have a a regular text field that I want to validate that im using for date of birth.

it's like dd/mm/yyyy. Im wondering how i can check that the user entered the date in the correct format with the slashes. I have a php script to calculate the age from the entered data and strips the slashes in order to do so. I figure i'd run with that, but just in case a user enters it wrong, well lol, you know how that is...

Any ideas?

Community
  • 1
  • 1
Sin
  • 265
  • 2
  • 7
  • 20

1 Answers1

2
if (!preg_match('#^(\d{2})/(\d{2})/(\d{4})$#', $date, $matches)) {
    die('Invalid format');
}

$dob = mktime(0, 0, 0, $matches[2], $matches[1], $matches[3]);

if (date('d/m/Y', $dob) != $date) {
    die('Invalid date');
}

echo 'User was born on ' . date('l, F jS, Y', $dob);
deceze
  • 510,633
  • 85
  • 743
  • 889
  • cool! now, one question so I can give this a go, how would I plug this to the field? Would I do a do_action or something else? Im working in wordpress, I should've noted that also – Sin Oct 07 '11 at 02:34
  • I don't know how you can integrate this into your project, since I don't know its structure. You should probably open a new question for that. – deceze Oct 07 '11 at 02:36
  • well I definitely think this is on the right track, I just need to direct it to the text input field. But if I take a look at the way wordpress validates normal fields, I can pretty much tie this to it I believe. Thanks again – Sin Oct 07 '11 at 02:54