使用angular2的DD/MM/YYYY格式的日期验证模式 [英] Validation pattern for date in DD/MM/YYYY format using angular2

查看:187
本文介绍了使用angular2的DD/MM/YYYY格式的日期验证模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用angular2 Validator.pattern为DD/MM/YYYY格式添加日期验证模式.

How to add validation pattern for Date in DD/MM/YYYY format using angular2 Validator.pattern.

我有必需的"验证器.找不到一种日期模式.代码如下:

I have the "required" validator in place. Cannot find one for date pattern. Code as below:

this.txtDob = new Control("", Validators.compose([Validators.required]));

推荐答案

没有内置的日期验证器.您必须创建一个自定义的 http ://blog.thoughtram.io/angular/2016/03/14/custom-validators-in-angular-2.html

There is no built-in date validator. You have to create a custom one http://blog.thoughtram.io/angular/2016/03/14/custom-validators-in-angular-2.html

您的验证器将如下所示 https://github.com/Angular2Buch/angular2-forms/blob/master/src/app/validators/date.validator.ts

Your validator will look like this https://github.com/Angular2Buch/angular2-forms/blob/master/src/app/validators/date.validator.ts

更新: 您需要为验证器提供类.在Validators.compose中将其指定为数组的第二个元素:

Update: You need to have class for the validator. Specify it as a second element of array in Validators.compose:

Validators.compose([Validators.required, MyValidator])

模式也可能起作用:如何在angular2中添加表单验证模式

这篇关于使用angular2的DD/MM/YYYY格式的日期验证模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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