Angular 2 Email Validator
Je vais avoir des ennuis avec ma simple e-mail validateur. Mon .ts page qui contrôle mon html avec ce code:
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])
});
}
}
Mon code HTML est:
<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>
Et enfin, mon e-mail.ts qui détient mon validateur ressemble à ceci:
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;
}
}
Je continue à avoir des erreurs lors de la référence à ce validateur dans ma main .fichier ts. Par exemple, j'obtiens cette erreur quand le curseur est sur "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
Ce que je fais mal?
source d'informationauteur Will
Vous devez vous connecter pour publier un commentaire.
Encore mieux, maintenant, Angulaire 4 a programme de validation d'email intégré
https://github.com/angular/angular/blob/master/CHANGELOG.md#features-6
https://github.com/angular/angular/pull/13709
Juste ajouter e-mail à la balise. Par exemple,
Cela a été résolu en changeant la classe importée de
Control
àFormControl
dans la première ligne de mon programme de validation.ts:Le modèle de l'attribut peut être utilisé pour la validation personnalisée
Suffit d'utiliser la
validator.pattern('^[^\s@]+@[^\s@]+\.[^\s@]{2,}$')
.ts code:
Code HTML: