Javascript AngularJS:如何验证美国格式的日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14146655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJS: How to validate date in US format?
提问by mikhail-t
I have the following form code that allows input of a date using AngularUI (date is required and should match US date format e.g.: MM/DD/YY):
我有以下表单代码,允许使用 AngularUI 输入日期(日期是必需的,并且应该与美国日期格式匹配,例如:MM/DD/YY):
<form name="form" ng-submit="createShipment()">
<!-- Shipment Date must be in MM/DD/YY format: -->
<input name="shipmentDate"
ng-pattern='/^[0,1]?\d{1}\/(([0-2]?\d{1})|([3][0,1]{1}))\/(([1]{1}[9]{1}[9]{1}\d{1})|([2-9]{1}\d{3}))$/'
ui-date="{ dateFormat: 'mm/dd/y' }" required ng-model="shipment.ShipmentDate" type="text">
<span ng-show="form.shipmentDate.$error.required">Date Required!</span>
<span ng-show="form.shipmentDate.$error.pattern">Incorrect Format, should be MM/DD/YY!</span>
<input class="btn-primary" ng-hide="!form.$valid" type="submit" value="Create">
</form>
Validation for requiredfield works fine, but date format is not being validated correctly and always shows 'Incorrect Format...'message.
对必填字段的验证工作正常,但未正确验证日期格式,并且始终显示“格式不正确...”消息。
I tried several different Regular Expressions that worked fine elsewhere, but it is still not working. Also I tried AngularUI validation and it doesn't work either. Thanks in advance!
我尝试了几种不同的正则表达式,它们在其他地方运行良好,但仍然无法正常工作。我也尝试过 AngularUI 验证,但它也不起作用。提前致谢!
UPDATE:
更新:
I figured that validation was conflicting with AngularUI datepicker I used, but datepicker autocorrects date anyway, so if datepicker is not used, than validation works as long as regular expression works, and if datepicker is used, there is not much need for other validation.
我认为验证与我使用的 AngularUI datepicker 冲突,但 datepicker 无论如何都会自动更正日期,所以如果不使用 datepicker,那么只要正则表达式有效,验证就可以工作,如果使用 datepicker,则不需要其他验证。
采纳答案by Will Sadler
The accepted answer doesn't work for me. I changed to:
接受的答案对我不起作用。我改为:
^(0?[1-9]|1[012])\/(0?[1-9]|[12][0-9]|3[01])\/((19\d{2})|([2-9]\d{3}))$
Otherwise only people born after 1990 need apply!
否则只有1990年以后出生的人才需要申请!
回答by Mark Rajcok
Your ng-pattern worked in a fiddleI created, but it allows for some incorrect dates, such as 0/9/1993 and 19/2/1993.
您的 ng-pattern 在我创建的小提琴中工作,但它允许一些不正确的日期,例如 0/9/1993 和 19/2/1993。
Here's a better pattern: (note, it was updated to match @WillSadler's answer)
这是一个更好的模式:(注意,它已更新以匹配@WillSadler 的答案)
^(0?[1-9]|1[012])\/(0?[1-9]|[12][0-9]|3[01])\/((19\d{2})|([2-9]\d{3}))$
小提琴。
回答by Ahosan Karim Asik
Check with leap year validate:Date format:dd-mm-yyyy
检查闰年验证:日期格式:dd-mm-yyyy
Regex pattern: ^(0?[1-9]|1\d|2[0-8]|29(?=[-]\d?\d[-](?!1[01345789]00|2[1235679]00)\d\d(?:[02468][048]|[13579][26]))|30(?![-]0?2)|31(?=[-]0?[13578]|[-]1[02]))[-](0?[1-9]|1[0-2])[-]([12]\d{3})$
正则表达式模式: ^(0?[1-9]|1\d|2[0-8]|29(?=[-]\d?\d[-](?!1[01345789]00|2[1235679]00)\d\d(?:[02468][048]|[13579][26]))|30(?![-]0?2)|31(?=[-]0?[13578]|[-]1[02]))[-](0?[1-9]|1[0-2])[-]([12]\d{3})$

