AngularJS:如何验证美国格式的日期? [英] AngularJS: How to validate date in US format?

查看:30
本文介绍了AngularJS:如何验证美国格式的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下表单代码,允许使用 AngularUI 输入日期(日期是必需的,并且应该匹配美国日期格式,例如:MM/DD/YY):

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):

<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 required field works fine, but date format is not being validated correctly and always shows 'Incorrect Format...' message.

我尝试了几种在其他地方运行良好的不同正则表达式,但仍然无法正常工作.我也尝试过 AngularUI 验证,但它也不起作用.提前致谢!

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 datepicker 存在冲突,但无论如何 datepicker 会自动更正日期,所以如果不使用 datepicker,那么只要正则表达式有效,验证就可以工作,如果使用 datepicker,则不需要太多其他验证.

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.

推荐答案

接受的答案对我不起作用.我改为:

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}))$

否则只有 1990 年以后出生的人才需要申请!

Otherwise only people born after 1990 need apply!

这篇关于AngularJS:如何验证美国格式的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆