我使用模型驱动创建了一个验证表单。这是我的验证器,用于检查电子邮件是否格式良好。
static emailValidator(control) {
if (control.value.match(/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/)) {
return null;
} else {
return { 'invalidEmailAddress': true };
}
}
我是这样使用它的:
constructor(private formBuilder: FormBuilder) {
//controlli campi della form
this.userForm = this.formBuilder.group({
'email': ['', [Validators.required, ValidationService.emailValidator]]
});
现在,我如何在我的验证器中传递一个参数,比如字符串?例如,我希望电子邮件不包含字符串"abcd“。
谢谢
发布于 2017-04-25 21:17:38
只需创建一个返回验证器函数的函数。
static emailValidator(match: string) {
return function (control: AbstractControl) {
// do validation here
}
}
用法
'email': ['', [Validators.required, ValidationService.emailValidator(someValue)]]
发布于 2017-04-18 20:17:41
试着这样做:
let EMAIL_REGEXP = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
if (EMAIL_REGEXP.test(control.value)) {
return null;
}
else {
return { 'invalidEmailAddress': true };
}
https://stackoverflow.com/questions/43467919
复制相似问题