【发布时间】:2021-07-19 02:08:59
【问题描述】:
我的简单电子邮件验证器出现问题。控制我的 html 的 .ts 页面具有以下代码:
import {EmailValidator} from '../../validators/email';
@Component({
templateUrl: 'build/pages/auth-signup/auth-signup.html',
})
export class AuthSignupPage {
constructor(private formBuilder: FormBuilder) {
this.slideOneForm = new FormGroup({
firstName: new FormControl('', [
Validators.maxLength(30),
Validators.pattern('[a-zA-Z ]*'),
Validators.required
]),
lastName: new FormControl('', [
Validators.maxLength(30),
Validators.pattern('[a-zA-Z ]*'),
Validators.required]),
email: new FormControl('', [
Validators.maxLength(30),
EmailValidator.isValidMailFormat,
Validators.required]),
password: new FormControl('', [
Validators.maxLength(30),
Validators.required]),
confirmPassword: new FormControl('', [
Validators.maxLength(30),
Validators.required])
});
}
}
我的 HTML 代码是:
<ion-item>
<ion-label floating>Email (this will be your login/username)</ion-label>
<ion-input #email (change)="elementChanged(email)" formControlName="email" type="email" [class.invalid]="!slideOneForm.controls.email.valid && (emailChanged || submitAttempt)"></ion-input>
</ion-item>
最后,保存验证器的 email.ts 如下所示:
import {Control} from '@angular/common';
export class EmailValidator {
static isValidMailFormat(control: Control){
var EMAIL_REGEXP = /^[a-z0-9!#$%&'*+\/=?^_`{|}~.-]+@[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$/i;
if (control.value != "" && (control.value.length <= 5 || !EMAIL_REGEXP.test(control.value))) {
return { "Please provide a valid email": true };
}
return null;
}
}
在我的主 .ts 文件中引用此验证器时,我一直出错。例如,将鼠标悬停在“EmailValidator.isValidMailFormat”上时出现此错误
[ts]
Argument of type '(ValidatorFn | ((control: Control) => { "Please provide a valid email": boolean; }))[]' is not assignable to parameter of type 'ValidatorFn | ValidatorFn[]'.
Type '(ValidatorFn | ((control: Control) => { "Please provide a valid email": boolean; }))[]' is not assignable to type 'ValidatorFn[]'.
Type 'ValidatorFn | ((control: Control) => { "Please provide a valid email": boolean; })' is not assignable to type 'ValidatorFn'.
Type '(control: Control) => { "Please provide a valid email": boolean; }' is not assignable to type 'ValidatorFn'.
Types of parameters 'control' and 'c' are incompatible.
Type 'AbstractControl' is not assignable to type 'Control'.
Property 'updateValue' is missing in type 'AbstractControl'.
import EmailValidator
我做错了什么?
【问题讨论】:
标签: angular typescript