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

查看:193
本文介绍了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天全站免登陆