开发者

month regex problem in php. validation problem

开发者 https://www.devze.com 2023-03-07 08:18 出处:网络
function preveriDatum($string) { if (preg_match(\"^(3)?(?(1)[0-1]|(0)?(?(2)[1-9]|[12][0-9]))\\.(0)?(?(3)[1-9]|1[0-2])\\.[1-9][0-9]{3}$^\", $string))
function preveriDatum($string)
{   
    if (preg_match("^(3)?(?(1)[0-1]|(0)?(?(2)[1-9]|[12][0-9]))\.(0)?(?(3)[1-9]|1[0-2])\.[1-9][0-9]{3}$^", $string)) 
        return true;
    else 
 开发者_开发知识库       return false;
}

is valid for 13.01.2010 but not for 13.1.2010. Can i aso make it valid for 13.1.2010?


Good grief, what are you trying to do here? Can you just put it into a DateTime class and verify it that way? http://php.net/manual/en/book.datetime.php

For the regex, in any case, you want a * instead of a ? for the 0. ? requires there to be at least one instance. Instead of (0)? use (0)*.


You could use:

if (preg_match("^(0?[1-9]|[12][0-9]|3[01])\.(0?[1-9]|1[012])\.([1-9][0-9]{3})$", $string)) 

Better is of course to use the DateTime class (http://php.net/manual/en/book.datetime.php).


Change .(0)?(?(3)[1-9]|1[0-2]) to .(0)?(?(3)[1-9]|1[0-2]|[1-9]). Also consider using a date library.

0

精彩评论

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

关注公众号