【问题标题】:Html5 date input - disallow partial date inputHtml5 日期输入 - 不允许部分日期输入
【发布时间】:2015-03-17 08:18:15
【问题描述】:

我有一个输入 type="date",这不是必需的。日期可以留空 (mm/dd/yyyy) 或有效。当用户只输入日期的一部分时(例如,月和日,没有年),它被默默地视为空,但有效。我想要的是将此视为无效,并提醒用户。这可能吗?

<input type="date" id="applicationDate" class="form-control" ng-model="emp.applicationDate">

【问题讨论】:

  • 您可以使用模式来验证它..
  • ng-pattern="\d{1,2}/\d{1,2}/\d{4}"

标签: angularjs html twitter-bootstrap-3


【解决方案1】:
 <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}))$/'
       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/YYYY</span>

    <input class="btn-primary" ng-hide="!form.$valid" type="submit" value="Create">

这是给你的小提琴:- http://jsfiddle.net/zo8aLh03/1/

【讨论】:

  • 如果你想要,你可以删除 required 我只是在表单末尾添加了按钮。
猜你喜欢
  • 2018-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多