-1

i'am trying to implement a function to check email and confirmation email with angular, when i type a different confirmation email, my function work and i get an error , now when i try to correct the email and make it same as the confirmation email i still get the error even if the two emails are same

the body of the function:

  static checkEmails(group: FormGroup): ValidationErrors | null {
    const email = group.controls.email.value;
    const confirmEmail = group.controls.confirmEmail.value;

    const responseKo = { NOT_SAME: true };
    const responseOk = null;

    let identical = false;
    if (email.toLowerCase() === confirmEmail.toLowerCase()) {
      identical = true;
    } else {
      group.controls.confirmEmail.setErrors(responseKo);
    }

    return identical ? responseOk : responseKo;
  }

1 Answers1

2

Just use a form validator for that ?

const emailChecker = (form: FormGroup) => {
  const email = form.get('email').value;
  const confirm = form.get('confirm').value;

  return email === confirm ? null : { emailConfirm: 'Email confirm mismatch' };
};

// ...

this.form = this.builder.group({ ... });
this.form.setValidators([emailChecker]);
  • thank you for the response could you be more specific? – dev_geek_ing Sep 23 '19 at 14:07
  • Well I thought my answer was ... What don't you understand ? –  Sep 23 '19 at 14:08
  • well, it doesn't work for me , still had the same issue – dev_geek_ing Sep 23 '19 at 14:39
  • It's easy to say that. Please provide a [mcve] reproducing the issue you have, it will be clearer and will help me explain you the issue on your code. –  Sep 23 '19 at 14:42
  • i updated the question , hope that it will be clear for you – dev_geek_ing Sep 23 '19 at 14:46
  • I have already provided the answer to your question, you said you don't understand it. So please, provide a [mcve] of your issue, so that I can explain it to you ! –  Sep 23 '19 at 14:50
  • thank you for the answer :) but it doesn't work for me , i understood what you had wrote – dev_geek_ing Sep 23 '19 at 14:55
  • and I understood it didn't work for you, that's why I'm askinf for a [mcve], to explain to you how it works !!! –  Sep 23 '19 at 14:56