Hi
I have Modal Popup . I want to put Validations . How it can e done
Python
Create New Student
Name
-->
Name is required
Student Name is required
-->
Mobile No
Email
City
State
PinCode
Address
Close -->
@if(studentObj.id ==0){
Save Student
}
@else {
Update Student
}export class Student {
id:number;
name:string;
mobileno:string;
email:string;
city:string;
state:string;
pincode:string;
address:string;
}
Thanks

Vikas SinghRank #441 · 5.7K reputationPosted May 13, 2024, 3:36 AM EDT
To add validations to your modal popup form, you can use Angular's built-in form validation features. Below is an example of how you can implement validation for the Name field:
Create New Student
In your component, make sure to import
NgFormandViewChildto handle form validation:import { Component, ViewChild } from '@angular/core';
import { NgForm } from '@angular/forms';
@Component({
selector: 'app-your-component',
templateUrl: './your-component.component.html',
styleUrls: ['./your-component.component.css']
})
export class YourComponent {
@ViewChild('myModal') modal: any;
studentObj: Student = new Student();
constructor() { }
// Function to submit the form
onSubmit(form: NgForm) {
if (form.valid) {
// Perform your save operation here
console.log("Form submitted successfully");
this.closeModal();
}
}
// Function to close the modal
closeModal() {
// Your modal close logic
}
}
export class Student {
id: number;
name: string;
mobileno: string;
email: string;
city: string;
state: string;
pincode: string;
address: string;
}
This code provides basic validation for the Name field, ensuring that it is required.