0

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

 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]]
      })
    });

This object contains all the validation messages for this form

 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.',
        },
      };

emailDomainError

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 };
  }
}

enter image description here

11
  • hi Abhiram i will help you with please start a chat with me and i will you with this Commented Nov 29, 2018 at 9:47
  • yeah did you understand my probmlem? Commented Nov 29, 2018 at 9:49
  • actully i understand what you trying to do but i need more info to help in this Commented Nov 29, 2018 at 9:51
  • what do you want? Commented Nov 29, 2018 at 9:52
  • can you update your code on stackblitz so i can update it Commented Nov 29, 2018 at 9:53

1 Answer 1

1

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;
  }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.