1

I'm trying to validate a date format in PHP "01/02" Day and Month. Anyway, i can't get this to work. Dose anyone know what i've done wrong in this case? It keeps saying my date is not valid for some reason..

if(!preg_match('/^((0[1-9])|(1[0-2]))\/(\d{2})$/',$postDate)) {
$array['error'] = 'true';
$array['errorMessage'] = 'Ugyldig dato (DD/MM)';
}
3
  • 3
    Do you want to match Day/Month (as you have written in your question) or Month/Day (as you have written in you correct seeming regex)? Commented Mar 15, 2011 at 14:08
  • oh lord, i got this all wrong! dude, thanks for seeing this, i must be blind! haha! Commented Mar 15, 2011 at 14:11
  • @nikic: Looks more like month/year to me. (unless the second number doesn't need validating) Commented Mar 15, 2011 at 14:11

1 Answer 1

1

Looks like you figured out your main problem, but I want to point out that the "day" part of your date pattern is pretty broad. I'd use something closer to this:

([012]\d|3[01])

That doesn't stop people from putting in stuff like 31/02 (i.e. February 31), though. That can be fixed, it just makes the regex longer. Let me know if you care about that and I'll edit.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the handsup Justin, i've just rewritten the regex, now it works great! Thanks anyway :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.