4

I've been fighting this for 2 days now. I just can't figure out what is wrong. It seems that my custom validators are not executed.

If you just press the 'Next' button every control is highlighted with red borders. If you fill the form, type different values in the email fields and press 'Next' the form is valid and I don't see any of the console.log's from the custom email validator.

https://stackblitz.com/edit/angular-nwhgpb

6
  • Sorry... This is my first stackblitz and I don't know how to add it properly... maybe someone can enlighten me.... Commented Jul 27, 2018 at 10:55
  • Anyone let me know, Where I can see the code? Commented Jul 27, 2018 at 10:55
  • I edited the url to stackblitz... this one should be the editor url. Commented Jul 27, 2018 at 10:57
  • For email use input type="email". Commented Jul 27, 2018 at 11:03
  • Thanks @ChatarSIngh ... Fixed it :-) Commented Jul 27, 2018 at 11:05

2 Answers 2

10

(IF you want to excute passwordValidator you need to add '()' to your Validator Function example password1: new FormControl("",[Validators.required,this.passwordValidator()]), Even though it will throw error because FormGroup you used inside will not have a reference to the formData password1 and password2)

If you want to compare two input field you need to use formGroup inside formGroup because if you do not use form group for the formGroup will not hold object like this.

value: Object
email1: "data"
email2: ""
__proto__: Object

https://angular.io/guide/form-validation#custom-validators Use This Ref for Implement ComparePassword and Email

/** A hero's name can't match the given regular expression */
export function forbiddenNameValidator(nameRe: RegExp): ValidatorFn {
  return (control: AbstractControl): {[key: string]: any} | null => {
    const forbidden = nameRe.test(control.value);
    return forbidden ? {'forbiddenName': {value: control.value}} : null;
  }; 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! You guided me down the right path :-D ... I have edited my stackblitz code, and it works like a charm. Should I include the final solution in my question... Is that good practice?
@Chellappan saved my day
I always forget this and kill like two hrs everytime
0

Use type="email" instead of type="text" in your input field unless the formgroup object does not know the field values is valid or not.

Check : https://stackblitz.com/edit/angular-eol5eq?file=src/app/app.component.html

2 Comments

This does not work, try email: a@a and repeat email: ab@a -> data valid.
You probably misunderstood my comment.. I fixed the email type, but this does not fix the issue. Email and password validators is still not executed...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.