I have this interface file:
userslist.ts
export interface Userslist {
id: number;
fname: string;
lname: string;
email: string;
pas1: string;
pas2: string;
}
I am using this interface to get form data including the field First Name, Last Name, email, password and confirm password.
The other two files:
ucreate.component.ts
import { Component, ViewChild, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Userslist } from '../userslist';
import { UserslistService } from '../userslist.service';
@Component({
selector: 'app-ucreate',
templateUrl: './ucreate.component.html',
styleUrls: ['./ucreate.component.css']
})
export class UcreateComponent implements OnInit {
// Declared the 'userForm' variable to store the user entered form data.
userForm: Userslist = {
id: 0,
fname: '',
lname: '',
email: '',
pas1: '',
pas2: '',
};
// Injected the 'UserslistService' and 'Router'.
constructor(private userlistService: UserslistService, private router: Router) { }
ngOnInit(): void {
}
// Invoking the API call to post the data.
create(){
this.userlistService.create(this.userForm)
.subscribe({
next:(data) => {
// On successful saving data to the server, we navigate to the adminhome page.
this.router.navigate(["/userslist/adminhome"])
},
error:(err) => {
console.log(err);
}
})
}
}
ucreate.component.html
<div class="container">
<legend>Create Item</legend>
<form>
<div class="mb-3">
<label for="txtName" class="form-label">First Name</label>
<input type="text" name="fname" [(ngModel)] = "userForm.fname" class="form-control" id="txtName" />
</div>
<div class="mb-3">
<label for="txtName" class="form-label">Last Name</label>
<input type="text" name="lname" [(ngModel)] = "userForm.lname" class="form-control" id="txtName" />
</div>
<div class="mb-3">
<label for="txtName" class="form-label">e-mail</label>
<input type="text" name="email" [(ngModel)] = "userForm.email" class="form-control" id="txtName" />
</div>
<div class="mb-3">
<label for="txtName" class="form-label">Password</label>
<input type="text" name="pas1" [(ngModel)] = "userForm.pas1" class="form-control" id="txtName" />
</div>
<div class="mb-3">
<label for="txtName" class="form-label">Confirm Password</label>
<input type="text" name="pas2" [(ngModel)] = "userForm.pas2" class="form-control" id="txtName" />
</div>
<button type="button" (click)="create()" class="btn btn-primary">Create Account</button>
</form>
</div>
How do I include custom validations into the input fields and also how do I show both the passwords are matching or not?
