开发者

PHP - Regex validate date

开发者 https://www.devze.com 2023-02-17 11:07 出处:网络
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 s

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)) {
$开发者_Go百科array['error'] = 'true';
$array['errorMessage'] = 'Ugyldig dato (DD/MM)';
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消