正則表達式生日驗證
問題描述:
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資源網 » 正則表達式生日驗證