Wil*_*ill 14 typescript angular
我的简单电子邮件验证器出了问题.控制我的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])
});
}
}
Run Code Online (Sandbox Code Playgroud)
我的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>
Run Code Online (Sandbox Code Playgroud)
最后,我的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;
}
}
Run Code Online (Sandbox Code Playgroud)
在我的主.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
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
so-*_*ude 17
更好的是,现在,Angular 4内置了电子邮件验证器 https://github.com/angular/angular/blob/master/CHANGELOG.md#features-6 https://github.com/angular/angular/pull/ 13709
只需添加电子邮件到标签.例如,
<form #f="ngForm">
<input type="email" ngModel name="email" required email>
<button [disabled]="!f.valid">Submit</button>
<p>Form State: {{f.valid?'VALID':'INVALID'}}</p>
</form>
Run Code Online (Sandbox Code Playgroud)
Wil*_*ill 11
这是解决通过更改导入的类Control来FormControl我validator.ts的第一行:
import {FormControl} from '@angular/forms';
export class EmailValidator {
static isValidMailFormat(control: FormControl){
let 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;
}
}
Run Code Online (Sandbox Code Playgroud)
Angular 9+ 和 Ionic 5+
默认 Angular 电子邮件验证器不足以满足我的用例。所以我用过这个:
表单.ts
this.form = this.formBuilder.group({
email: ['', Validators.compose([Validators.required,
Validators.pattern(RegularExpressionConstant.EMAIL)])],
});
Run Code Online (Sandbox Code Playgroud)
正则表达式.constant.ts
export class RegularExpressionConstant {
static EMAIL: RegExp = /^(([^<>()\[\]\\.,;:\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,}))$/;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
32109 次 |
| 最近记录: |