开发者

month regex problem in php. validation problem

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜