简体   繁体   中英

Regular Expression in mm/dd/yyyy format

Below is the regular expression for mm/dd/yyyy format. BUT the expression is giving true for mmddyyyy format also. It should return false without '/' .

Anyone can change it to the required regex?

/^(((0[13-9]|1[012])[-/]?(0[1-9]|[12][0-9]|30)|(0[13578]|1[02])[-/]?31|02[-/]?(0[1-9]|
1[0-9]|2[0-8]))[-/]?[0-9]{4}|02[-/]?29[-/]?([0-9]{2}(([2468][048]|[02468][48])|[13579][26])|
([13579][26]|[02468][048]|0[0-9]|1[0-6])00))$/

This is the regex you want

/^(((0[13-9]|1[012])[-/](0[1-9]|[12][0-9]|30)|(0[13578]|1[02])[-/]31|02[-/](0[1-9]|1[0-9]|2[0-8]))[-/][0-9]{4}|02[-/]29[-/]([0-9]{2}(([2468][048]|[02468][48])|[13579][26])|([13579][26]|[02468][048]|0[0-9]|1[0-6])00))$/

[-/]? will check for 0 or 1 occurrence of '/' in your string. [-/] will check exactly 1 occurrence instead.

Try this RegEx, its working

 var date = "21062016";//"21/06/2016"; var rgexp = /(^(((0[1-9]|1[0-9]|2[0-8])[/](0[1-9]|1[012]))|((29|30|31)[/](0[13578]|1[02]))|((29|30)[/](0[4,6,9]|11)))[/](19|[2-9][0-9])\\d\\d$)|(^29[/]02[/](19|[2-9][0-9])(00|04|08|12|16|20|24|28|32|36|40|44|48|52|56|60|64|68|72|76|80|84|88|92|96)$)/; var validate = rgexp.test(date); alert(validate); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM