AngularJS表单自定义验证(未来日期) [英] AngularJS form custom validation (future date)

查看:88
本文介绍了AngularJS表单自定义验证(未来日期)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的AngularJS应用添加验证,并且我想测试某个特定日期是否在将来。如果我有2个选择,这样做的最佳做法是什么?

I am trying to add validation to my AngularJS app and I want to test if a specific date is in the future. What is the best practice to do so if I have 2 selects for this?

例如:

<select name="expirationYear"
   ng-model="expYear"
   required>
   <option ng-repeat="n in [] | range:21" value="{{currentYear+n}}">{{currentYear+n}}</option>
</select>

<select name="expirationMonth"
   ng-model="expMotnh"
   required>
   <option ng-repeat="n in [] | range:12" value="{{n+1}}">{{n+1}}</option>
</select>

我想添加一个自定义规则来验证日期,如果它在将来而不在过去。

I want to add a custom rule to validate the date if it is in the future and not in the past.

推荐答案

Demo Plunker

您可以创建依赖于 ngModel 的自定义指令:

You can create a custom directive that relies on ngModel:

<form name="form"> 
  <date-picker name="date" ng-model="date"></date-picker> 
  <span class="error" ng-show="form.date.$error.futureDate">
       Error: Date is in the future!
  </span>
</form>

该指令将创建一个隔离范围来创建私有范围,并需要ngModel和可选的父表单:

The directive would create an isolated scope to create a private scope, and require ngModel and an optional parent form:

require: ['ngModel', '^?form'],
scope: { }

指令的控制器将初始化下拉列表的年份和月份:

The directive's controller would initialize years and months for the drop down lists:

controller: function($scope){
   $scope.years = [1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018];
   $scope.months = ['Jan','Feb', 'Mar', 'Apr', 'May','Jun', 'Jul','Aug', 'Sep', 'Oct','Nov','Dec']        
}

该指令将呈现以下模板:

The directive would render the following template:

template: '<select ng-model="year" ng-options="y for y in years"></select>' 
        + '<select ng-model="month" ng-options ="m for m in months"></select>'

设置 $ watch 以设置月份或年份下拉时的日期更改:

Set up a $watch to set the date whenever the month or year drop-down changes:

scope.$watch('year', function(year) {
  if (year) {
      var month = scope.months.indexOf(scope.month);
      ngModel.$setViewValue(new Date(year, month,1));
  }
});
scope.$watch('month', function(month) {
  if (month) {
      var year = scope.year;
      var monthIndex = scope.months.indexOf(scope.month);
      ngModel.$setViewValue(new Date(year, monthIndex,1));
  }
});
ngModel.$formatters.push(function(val) {
  scope.year = val.getFullYear();
  scope.month = scope.months[val.getMonth()];
});

使用ngModel控制器添加futureDate验证器:

Use the ngModel controller to add a futureDate validator:

ngModel.$validators.futureDate = function(val) {
  var date = new Date();
  return val <= new Date(date.getFullYear(), date.getMonth(),1);
}

然后您可以使用AngularJS表单验证:

You can then use AngularJS form validation:

if ($scope.form.date.$valid) {
     ...
}
if ($scope.form.date.$error.futureDate) {
     ...
}
etc.

这篇关于AngularJS表单自定义验证(未来日期)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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