正则表达式生日验证
问题描述:
I have a regex which validate birthday:
1 2 3 4 5 |
if(!preg_match(\'/^(0?[1-9]|1[012])[- .\\/](0?[1-9]|[12][0-9]|3[01])[- .\\/](19|20)?[0-9]{2}$/\', $_POST[\'bday\']) { echo \'enter your birthdate in a valid format. mm/dd/yyy\'; } |
which is:
correct month
leap year date is not accurate
february accepts day 30 & 31
and the year 20 accepts greater than 2013
I only done correct with months how can I do more accurate birthday validation? or there is any alternate way to validate birthday?
my desired output is:
validate leap year date on month of february
year must not greater than 2010
OUTPUT:
02/14/2010
I\'m just quite new on regex kindly help?
图片转代码服务由CSDN问答提供
感谢您的意见,我们尽快改进~
功能建议
我有一个验证生日的正则表达式:</ p>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
if( !preg_match(\'/ ^(0?[1-9] | 1 [012])[ - 。\\ /](0?[1-9] | [12] [0-9] | 3 [01])[ - 。\\ /](19 | 20)?[0-9] {2} $ /\',$ _POST [\'bday\']) { echo\'以有效格式输入您的出生日期.mm / dd / yyy\'; } </ code> </ pre> <p>这是:</ p> <p> <strong>正确的月份</ strong> </ p> <p> <em>闰年日期不准确</ em> </ p> <p> <em> 2月接受第30天和第31天</ em> </ p> \\ n <p> <em>并且第20年接受超过2013年</ em> </ p> <p>我只用了几个月就更正确如何才能更准确地进行生日验证?或者有更正确的生日验证? 验证生日的任何其他方式?</ p> <p>我想要的输出是:</ p> <p> <strong>验证2月份的闰年日期</ strong> < / p> <p> <strong>年份不得大于2010年</ strong> </ p> <p>输出:</ p> <p> <code> 02/14/2010 </ code> </ p> <p>我对正则表达式的帮助很新吗?</ p> </ div> <h3>网友观点:</h3> <div> <p>A regex isn\'t well-suited for parsing dates. PHP already has built-in functions and classes to do this and you should use that instead. Here\'s how this can be done using PHP\'s <a>DateTime</a> class (function by <a>Glavić</a>, from <a>php.net</a>):</p> <pre><code>function validateDate($date, $format = \'m/d/Y\') { $d = DateTime::createFromFormat($format, $date); return $d && $d->format($format) == $date && $d->format(\'Y\') <= 2010; } |
For example:
1 2 3 |
var_dump(validateDate(\'02/14/2010\')); // bool(true) var_dump(validateDate(\'02/14/2011\')); // bool(false) |
请问判断验证一个字符串是正则表达式的正则表达式要怎么写?
高赞 @陈壳子 的回答很难说是正确的(而且还不注明引用来源。。),然而这位关了评论只能另开回答,理由如下:
- 除非我们对”正则表达式“这个概念没有达成共识,提问者的问题应该等价于“一个由合法正则表达式组成的语言是否是正则的,如果是,给出实现”,然而答案是否定的,因为判断正则表达式是否合法时要解决的一个更小的问题是判断括号是否匹配(更正式的说,括号匹配问题可以规约到合法正则表达式的判定问题),然而括号匹配不是正则的,可以用pumping lemma,取 s=0^p1^p 来证明。
- 事实上原Stackoverflow回答下面紧接着一句:
This is a recursive regex, and is not supported by many regex engines.
从工程上来讲,这个方法“is not supported by many regex engines”,应该不太实用;从形式语言的角度来讲问题比较大,因为recursive "regular" expression is not regular.
那位引用的Stackoverflow回答 - Is there a regular expression to detect a valid regular expression?
也是Stackoverflow - Why is recursive regex not regex?
1. 带 [亲测] 说明源码已经被站长亲测过!
2. 下载后的源码请在24小时内删除,仅供学习用途!
3. 分享目的仅供大家学习和交流,请不要用于商业用途!
4. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
5. 本站所有资源来源于站长上传和网络,如有侵权请邮件联系站长!
6. 没带 [亲测] 代表站长时间紧促,站长会保持每天更新 [亲测] 源码 !
7. 盗版ripro用户购买ripro美化无担保,若设置不成功/不生效我们不支持退款!
8. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
9. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
10.如果您购买了某个产品,而我们还没来得及更新,请联系站长或留言催更,谢谢理解 !
GG资源网 » 正则表达式生日验证