Recently delving into Angular 8, I find myself crafting a login component with the intention of redirecting to another component upon entering the correct username and password. Here's what my code looks like so far:
Here is the TypeScript for my login component:
import { Component, OnInit } from '@angular/core';
import{FormGroup,FormBuilder,Validators} from '@angular/forms'
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
myForm:FormGroup
submitted=false;
_baseURL : string;
formvalid: boolean;
user:string;
pwd:string;
constructor(private formbuilder:FormBuilder){}
ngOnInit(){
this.myForm = this.formbuilder.group({
username:['',Validators.required],
password:['',[Validators.required,Validators.pattern("^(?=.*?[a-z])(.{13,}|(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,12})$")]]
});
this._baseURL="";
this.formvalid=false;
}
Submitme(){
this.submitted = true;
if (this.myForm.invalid) {
this.formvalid=false;
return;
}
if (this.myForm.valid) {
this.formvalid=true;
this._baseURL= 'Welcome'+" "+ this.myForm.controls.username.value;
}
this.user=this.myForm.controls.username.value;
this.pwd=this.myForm.controls.password.value;
}
rest(){
this.myForm.reset();
this.submitted = false;
}
}
And here is the HTML for my login component:
<h1 class="text-center" [hidden]="formvalid">Login</h1>
<form [formGroup]="myForm" (ngSubmit)="Submitme()" class="text-center">
<div class="container bg-dark" [hidden]="formvalid" style="border: 1px solid black;">
<div class="row mt-3">
<div class="col-lg-5">
<label for="name" class="text-white">Username :</label>
</div>
<input type="text" formControlName="username" class="col-lg-4 form-control"
[ngClass]="{ 'is-invalid': submitted && myForm.controls.username.errors }">
<div *ngIf="submitted && myForm.controls.username.errors" class="text-danger">
<div *ngIf="myForm.controls.username.errors.required">First Name is required</div>
</div>
</div>
<div class="row mt-2">
<div class="col-lg-5">
<label for="pwd" class="text-white">Password :</label>
</div>
<input type="password" formControlName="password" class="col-lg-4 form-control"
[ngClass]="{ 'is-invalid': submitted && myForm.controls.password.errors }">
<div *ngIf="submitted && myForm.controls.password.errors" class="text-danger">
<div *ngIf="myForm.controls.password.errors.required">password is required</div>
<div *ngIf="myForm.controls.password.errors.pattern">Pwd must contain atleast one upper and
lower case character one special character one digit and 8 characters in length </div>
</div>
</div>
<button class="btn btn-primary mt-2 mb-3" type="submit"
[routerLink]="'/afterlog'">Submit</button>
<input type="button" (click)="rest()" value="Reset" class="btn btn-primary mt-2 mb-3">
</div>
</form>
Upon successful login, the goal is to navigate to another component displaying a welcome message through Angular 8 routing mechanisms. Appreciate any assistance!