Dexty
Dexty

Reputation: 1472

PHP - Regex validate date

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

Upvotes: 1

Views: 638

Answers (1)

Justin Morgan
Justin Morgan

Reputation: 30750

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.

Upvotes: 1

Related Questions