在反应形式上使用正则表达式进行Angular 7电子邮件验证 [英] Angular 7 email validation using regex on reactive form
本文介绍了在反应形式上使用正则表达式进行Angular 7电子邮件验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Angular上,我正尝试使用以下正则表达式来验证电子邮件-
On Angular, I am trying to validate email using following regex -
^(([^<>()[\]\\.,;:\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,}))$')
]))
});
}
但是看起来好像没有用.即使有效,它始终显示电子邮件无效".
But looks like it's not working. It always display "Email is invalid.", even though it is valid.
推荐答案
我在这里举了一个例子: https ://stackblitz.com/edit/angular-pgc7st
I made an example here: https://stackblitz.com/edit/angular-pgc7st
因此在Validator中应该是这样的:
So in the Validator it should be like this:
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('')就可以了.检查示例.
If you see in you code you are entering an string as reexp. so removing the string char ('') it works. Check the example.
这篇关于在反应形式上使用正则表达式进行Angular 7电子邮件验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文