I am trying enable the login button based on the email validations using angular's Validators.email. However, even after entering all the required fields the button is not getting enabled. I also tried using validators.minlength but there as well I am facing the same challenge. Thank you in advance for the help.
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators, FormControl } from '@angular/forms';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
myForm!: FormGroup;
constructor(private fb: FormBuilder) {
this.submitForm();
}
ngOnInit(): void {
}
submitForm() {
this.myForm = this.fb.group({
name: ['', Validators.required, Validators.email ],
password: ['', Validators.required]
});
}
get name(){
return this.myForm.get('name');
}
}
<div fxLayoutAlign="center center" fxFlexFill class="main-div">
<mat-card fxFlex="25">
<mat-toolbar color="primary">LogIn Page</mat-toolbar>
<form fxLayoutAlign="stretch" fxLayout="column" class="login-form" [formGroup]="myForm">
<mat-form-field>
<mat-label>User Name</mat-label>
<input FormControlName="name" matInput placeholder="name">
</mat-form-field>
<mat-form-field>
<mat-label>Password</mat-label>
<input matInput FormControlName="password" placeholder="password" type="password">
</mat-form-field>
<button mat-raised-button type="submit" [disabled]="!myForm.valid" routerLink="/mainPage">LogIn</button>
</form>
</mat-card>
</div>