Hi
I have Modal Popup . I want to put Validations . How it can e done
export class Student {
id:number;
name:string;
mobileno:string;
email:string;
city:string;
state:string;
pincode:string;
address:string;
}
Thanks
Hi
I have Modal Popup . I want to put Validations . How it can e done
export class Student {
id:number;
name:string;
mobileno:string;
email:string;
city:string;
state:string;
pincode:string;
address:string;
}
Thanks
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Vikas SinghPosted May 13, 2024, 7:36 AM
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.