开发者

Having input be specifically in MM/DD/YYYY format

How would i apply an if statement i开发者_StackOverflown an input field if i want the input to be exactly in MM/DD/YYYY format? Any other input that is different from this format i would echo out an error statement.


You could use a regular expresion for that:

if (preg_match('#^\d\d/\d\d/\d\d\d\d$#', $str)) {

The # are just preg_match delimiters. The ^ and $ mark start and end for the subject to match. And each \d is a placeholder which will match a decimal. The / are literal slashes which preg_match will test for.

You could also add ( capture ) groups to test the individual decimal blocks for conformity. I would do that separately to check for alternating formats. But for your purposes, it could be:

if (preg_match('#^(0\d|10|11|12)/([0-2]\d|30|31)/(19|20)\d\d$#', $str)) {


Why not use a dropdown box? One for the month, one for the day, one for the year.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜