在反应式表单上使用正则表达式进行 Angular 7 电子邮件验证

2023-12-14

在 Angular 上,我尝试使用以下正则表达式验证电子邮件 -

^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$

就像下面这样——

createGroupForm() {
    this.childGroupForm = new FormGroup({
        'groupName': new FormControl(null, Validators.compose([
            Validators.required
        ])),
        'groupEmail': new FormControl(null, Validators.compose([
            Validators.pattern('^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$')
        ]))
    });
}

但看起来它不起作用。它总是显示“电子邮件无效。”,即使它是有效的。


我在这里举了一个例子:https://stackblitz.com/edit/angular-pgc7st

所以在验证器中应该是这样的:

Validators.pattern(/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/)

如果您在代码中看到您正在输入一个字符串作为 reexp。所以删除字符串 char ('') 就可以了。检查示例。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在反应式表单上使用正则表达式进行 Angular 7 电子邮件验证 的相关文章

随机推荐