[英]how to validate datefield in angular reactive form?
I am trying to validate my form in angular using reactive form .There is few fields example date
and year
in which user enter only number
date formate (01 -31) year formate (1990 ---or 2019 four digit number) so he try to enter anything other than string I need to show invalid message .I tried like this 我正在尝试使用反应形式以角度验证我的表格。很少有字段示例
date
和year
,其中用户仅输入number
日期格式(01 -31)年格式(1990 ---或2019四位数),因此他尝试输入除字符串以外的任何内容,我需要显示无效消息。
I am looking what is pattern of date
and year
pattern 我在看什么是
date
和year
模式
https://codesandbox.io/s/jzq6nn6zz9 https://codesandbox.io/s/jzq6nn6zz9
cardnumber: ["", [Validators.required, Validators.pattern("[0-9]{16}")]],
dateofexipiredate: ["", [Validators.required]],
dateofexipireyear: [""],
cvc: ["", [Validators.required, Validators.pattern("[0-9]{3}")]]
what is pattern of dateofexipiredate
and dateofexipireyear
dateofexipiredate
和dateofexipireyear
模式是什么
You can create something like the following to cover the spec you have mentioned: 您可以创建类似于以下内容的内容,以涵盖您提到的规格:
For the year: Validators.pattern("(199[0-9])|(20[01][0-9])")
一年中:
Validators.pattern("(199[0-9])|(20[01][0-9])")
For the date: Validators.pattern("(0[1-9])|([12][0-9])|(3[0-1])")
对于日期:
Validators.pattern("(0[1-9])|([12][0-9])|(3[0-1])")
However, it might be better to consider validating these two together with the month to ensure it is actually a valid date - eg there is no 31/02/2013. 但是,最好将两个月份与月份一起验证,以确保它实际上是一个有效日期-例如,没有2013年2月31日。 That would be much harder using just a regular expression though (particularly leap years and so forth).
但是,仅使用正则表达式会更困难(尤其是leap年等等)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.