Angular中的日期和货币验证(4) [英] Date and Currency validation in Angular (4)
问题描述
我是Angular的新手.我正在使用4号反应式表格,并弄清楚了如何执行自定义验证.以下是我对数字的实现
I am new to Angular. I am using angular 4 reactive forms and figured out how to perform custom validations. Following is my implementation for numeric
function numberValidator(c: AbstractControl): { [key: string]: boolean } | null {
if (c.pristine) {
return null;
}
if (c.value.match(/.*[^0-9].*/)) {
return { 'numeric': true };
}
return null;
}
phoneControl: ['', [Validators.required, Validators.minLength(10), Validators.maxLength(10), numberValidator]],
我正在尝试找出如何执行货币(有无小数点后两位),最重要的是日期"字段.
I am trying to find out how to perform currency (with or without two decimal places) and most importantly Date field.
如果在其他地方回答了这个问题,请原谅我,但我找不到关于angular(4)的任何示例
Forgive me if this was answered elsewhere but I cannot find any samples for angular(4)
感谢您的时间
推荐答案
您需要哪种日期验证?只是值是一个有效日期?如果在输入元素上设置type="date"
,则浏览器将确保仅输入有效日期.
What kind of date validation do you need? Just that the value is a valid date? If you set the type="date"
on the input element the browser will ensure that only a valid date is entered.
与您显示的数字验证器和任何货币验证器相同.您应该能够在输入元素上设置type="number"
,并且不需要验证器.
Same for your shown number validator and for any currency validator. You should be able to set the type="number"
on the input element and you won't need a validator.
如果您仍然想执行自己的验证,则可以像示例中一样使用正则表达式.
If you still do want to perform your own validation, you can use regular expressions just as in your example.
只需查找正则表达式以获取货币和日期.对于日期,是这样的: JavaScript-用于验证日期格式的正则表达式
Just look up regular expressions for currency and date. For the date, something like this: Javascript - Regex to validate date format
这篇关于Angular中的日期和货币验证(4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!