adspace


How to validate form in angular 4?

Answer Posted / Musabbir Ali

In Angular 4, you can use FormsModule and ReactiveFormsModule for form validation. Here's an example:nnTemplate:n```htmln<form [formGroup]="myForm">n <input formControlName="username">n <input formControlName="password">n <button (click)="submit()">Submit</button>n</form>n```nComponent TS:n```typescriptnimport { FormBuilder, Validators } from '@angular/forms';n...nconstructor(private fb: FormBuilder) {n this.myForm = this.fb.group({n username: ['', Validators.required],n password: ['', [Validators.required, Validators.minLength(6)]]n});n}nsubmit() {n if (this.myForm.valid) {n console.log('Form submitted!');n } else {n console.log('Invalid form!');n }n}n```

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the latest angular?

811


How to use primeng in angular 4?

951


What is the latest version of angular?

982


How to enable html5 mode in angular 1.x?

931


Angularjs dependency injection of value inside of the module.config?

897


Which version you are using js?

946


How can primeng be used with angular2?

949


What is a dynamic route in angularjs and how do you implement it?

869


How to implement a datagrid table with angular 2 and primeng?

897


How to get and log an error in angular 4?

881


How to create angularjs filter which outputs html?

863


How an object or function can get a hold of its dependencies?

857


Give the example of angularjs strings?

878