Reputation: 2061
I am new for angular and i have created one Custom EmailDomainError validator class and its working fine.
But problem is Email domain Error is showing along with Email validation error message how can i resolve this problem i am really confusing can some one help me please
I think i have to check email pattern is correct or not in my EmailDomainError class how can i ckeck that?if i think correct way and my complete code---https://stackblitz.com/edit/angular-bvihqj
ngOnInit() {
this.employeeForm = this.fb.group({
fullName: ['', [Validators.required, Validators.minLength(2), Validators.maxLength(10)]],
contactPreference: ['email'],
email: ['', [Validators.required, Validators.email, emailDomainError]],
phone: [''],
skills: this.fb.group({
skillName: ['', [Validators.required]],
experienceInYears: ['', [Validators.required]],
proficiency: ['', [Validators.required]]
})
});
formErrors = {
'fullName': '',
'email': '',
'phone': '',
'skillName': '',
'experienceInYears': '',
'proficiency': ''
};
validationMessages = {
'fullName': {
'required': 'Full Name is required.',
'minlength': 'Full Name must be greater than 2 characters.',
'maxlength': 'Full Name must be less than 10 characters.'
},
'email': {
'required': 'Email is required.',
'email': 'Valid Email id is required.',
'emailDomainError': 'Email domain should be karvy.com'
},
'phone': {
'required': 'Phone number is required.'
},
'skillName': {
'required': 'Skill Name is required.',
},
'experienceInYears': {
'required': 'Experience is required.',
},
'proficiency': {
'required': 'Proficiency is required.',
},
};
function emailDomainError(control: AbstractControl): { [key: string]: any } | null {
const email: string = control.value;
const domain: string = email.substring(email.lastIndexOf("@") + 1);
if (email === '' || domain === "karvy.com") {
return null;
} else {
return { "emailDomainError": true };
}
}
Upvotes: 0
Views: 49
Reputation: 883
Hi AbhiRam I Update your code here is link for Here
In Form Group Update this like
email: ['', [Validators.required, emailError, emailDomainError]],
Function Code change like this *
*
* emailDomainValidations
* @param control
*/
function emailDomainError(control: AbstractControl): { [key: string]: any } | null {
const email: string = control.value;
if (email && email.indexOf("@") != -1) {
let [_, domain] = email.split("@");
if (domain !== "karvy.com") {
return {
"emailDomainError": true,
// "email": false
};
}
else {
return null;
}
}
}
function emailError(control: AbstractControl): { [key: string]: any } | null {
const email: string = control.value;
if( email.length <3 && email.length >=1){
return { "email" : true}
}
else{
return null;
}
}
Upvotes: 1