Angular 中的日期和货币验证 (4) [英] Date and Currency validation in Angular (4)

查看:27
本文介绍了Angular 中的日期和货币验证 (4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Angular 的新手.我正在使用 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)

感谢您的时间

推荐答案

您需要什么样的日期验证?只是该值是一个有效的日期?如果您在 input 元素上设置 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屋!

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