Gathering detailed insights and metrics for ng-bootstrap-form-validation
Gathering detailed insights and metrics for ng-bootstrap-form-validation
Gathering detailed insights and metrics for ng-bootstrap-form-validation
Gathering detailed insights and metrics for ng-bootstrap-form-validation
@mingleats/ng-bootstrap-form-validation
ng-bootstrap-form-validation-sittax
An Angular module that makes Bootstrap form validation easy.
ng-form-group
Automatic Bootstrap validation based on ngModel
@teamteanpm2024/maxime-unde-voluptas
**[angular's nicest part](https://github.com/angular/angular.js/blob/6b049c74ccc9ee19688bb9bbe504c300e61776dc/src/ng/parse.js) extracted as a standalone module for the browser and node.**
An Angular Module for easy data driven (reactive) form validation
npm install ng-bootstrap-form-validation
Typescript
Module System
Node Version
NPM Version
74.1
Supply Chain
92.5
Quality
75.7
Maintenance
50
Vulnerability
98.9
License
TypeScript (76.17%)
HTML (16.44%)
JavaScript (7.21%)
SCSS (0.18%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
58 Stars
175 Commits
41 Forks
7 Watchers
27 Branches
4 Contributors
Updated on Jun 25, 2025
Latest Version
9.0.1
Package Id
ng-bootstrap-form-validation@9.0.1
Unpacked Size
302.98 kB
Size
68.63 kB
File Count
50
NPM Version
6.9.0
Node Version
10.16.3
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
2
An Angular module that makes Bootstrap form validation easy.
Check out the demo!
Note: v9.0.0 is out and supports Angular 9!
Install by running npm install ng-bootstrap-form-validation --save
Add NgBootstrapFormValidationModule.forRoot()
to your app.module.ts
imports:
1import { BrowserModule } from '@angular/platform-browser'; 2import { NgModule } from '@angular/core'; 3import { FormsModule , ReactiveFormsModule} from '@angular/forms'; 4import { AppComponent } from './app.component'; 5 6import { NgBootstrapFormValidationModule } from 'ng-bootstrap-form-validation'; 7 8@NgModule({ 9 declarations: [ 10 AppComponent 11 ], 12 imports: [ 13 BrowserModule, 14 FormsModule, 15 ReactiveFormsModule, 16 NgBootstrapFormValidationModule.forRoot() 17 ], 18 providers: [], 19 bootstrap: [AppComponent] 20}) 21export class AppModule { }
NgBootstrapFormValidationModule
to other modules in your application:1import { NgModule } from '@angular/core'; 2import { OtherComponent } from './other.component'; 3 4import { NgBootstrapFormValidationModule } from 'ng-bootstrap-form-validation'; 5 6@NgModule({ 7 declarations: [ 8 OtherComponent 9 ], 10 imports: [ 11 NgBootstrapFormValidationModule 12 ] 13}) 14export class OtherModule { }
Note:
If you are only using a single (app
) module, then you will need to import both:
1import { NgBootstrapFormValidationModule } from 'ng-bootstrap-form-validation'; 2 3@NgModule({ 4 declarations: [ 5 AppComponent 6 ], 7 imports: [ 8 ... 9 NgBootstrapFormValidationModule.forRoot(), 10 NgBootstrapFormValidationModule 11 ], 12 providers: [], 13 bootstrap: [AppComponent] 14}) 15export class AppModule { }
By default, the validators found on the Validators
class from @angular/forms
module are handled for you out of the box. All you need to do is import the module.
ng-bootstrap-form-validation works by using the form-group
Bootstrap class on your divs as component selector, and projecting the content into a component which handles form validation feedback for you.
The has-error
and has-success
classes are automatically added or removed to your form-group
based on whether or not the input is valid, and is both touched
and dirty
.
Validation messages appear when an input is dirty
, touched
, and has errors.
Submitting the form will iterate over all controls and mark them as touched
and dirty
to provide feedback to the user. Additionally, there is a validSubmit
event on forms which you can bind to instead of submit
to only fire off when the form is actually valid.
basic-example.component.ts
1import {Component, OnInit} from "@angular/core";
2import {FormControl, FormGroup, Validators} from "@angular/forms";
3
4@Component({
5 selector: 'app-basic-example',
6 templateUrl: './basic-example.component.html',
7 styleUrls: ['./basic-example.component.css']
8})
9export class BasicExampleComponent implements OnInit {
10
11 formGroup: FormGroup;
12
13 ngOnInit() {
14 this.formGroup = new FormGroup({
15 Email: new FormControl('', [
16 Validators.required,
17 Validators.pattern(/^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/)
18 ]),
19 Password: new FormControl('', [
20 Validators.required,
21 Validators.minLength(8),
22 Validators.maxLength(20)
23 ])
24 });
25 }
26
27 onSubmit() {
28 console.log(this.formGroup);
29 }
30
31 onReset() {
32 this.formGroup.reset();
33 }
34
35}
basic-example.component.html
1<div class="row"> 2 <div class="col-md-6 col-md-offset-3"> 3 <form [formGroup]="formGroup" (validSubmit)="onSubmit()"> 4 <div class="form-group"> 5 <label class="control-label">Email</label> 6 <input type="text" class="form-control" formControlName="Email"> 7 </div> 8 <div class="form-group"> 9 <label class="control-label">Password</label> 10 <input type="password" class="form-control" formControlName="Password"> 11 </div> 12 <button class="btn btn-default" type="button" (click)="onReset()">Reset</button> 13 <button class="btn btn-primary pull-right" type="submit">Submit</button> 14 </form> 15 </div> 16</div>
Note: the <bfv-messsages></bfv-messages>
component still must be placed within the <div class="form-group">
.
basic-example.component.html
1<div class="row"> 2 <div class="col-md-6 col-md-offset-3"> 3 <form class="form-horizontal" [formGroup]="formGroup" (validSubmit)="onSubmit()"> 4 <div class="form-group"> 5 <label class="control-label col-sm-2">Email</label> 6 <div class="col-sm-10"> 7 <input type="text" class="form-control" formControlName="Email"> 8 <bfv-messages></bfv-messages> 9 </div> 10 </div> 11 <div class="form-group"> 12 <label class="control-label col-sm-2">Password</label> 13 <div class="col-sm-10"> 14 <input type="password" class="form-control" formControlName="Password"> 15 <bfv-messages></bfv-messages> 16 </div> 17 </div> 18 <button class="btn btn-default" type="button" (click)="onReset()">Reset</button> 19 <button class="btn btn-primary pull-right" type="submit">Submit</button> 20 </form> 21 </div> 22</div>
You can provide an ErrorMessage
array via the CUSTOM_ERROR_MESSAGES
multi-provider in your module to provide custom errors across your module/app. In order for this to be AOT compatable, the function definitions must be exported. see below for an example
custom-errors.ts
1import {ErrorMessage} from "ng-bootstrap-form-validation"; 2 3export const CUSTOM_ERRORS: ErrorMessage[] = [ 4 { 5 error: "required", 6 format: requiredFormat 7 }, { 8 error: "email", 9 format: emailFormat 10 } 11]; 12 13export function requiredFormat(label: string, error: any): string { 14 return `${label} IS MOST DEFINITELY REQUIRED!`; 15} 16 17export function emailFormat(label: string, error: any): string { 18 return `${label} doesn't look like a valid email address.`; 19}
app.module.ts
1import {BrowserModule} from "@angular/platform-browser"; 2import {NgModule} from "@angular/core"; 3import {FormsModule, ReactiveFormsModule} from "@angular/forms"; 4import { HttpClientModule } from '@angular/common/http'; 5import { 6 NgBootstrapFormValidationModule, 7 CUSTOM_ERROR_MESSAGES 8} from "ng-bootstrap-form-validation"; 9import {AppComponent} from "./app.component"; 10import {CUSTOM_ERRORS} from "./custom-errors"; 11 12@NgModule({ 13 declarations: [ 14 AppComponent 15 ], 16 imports: [ 17 BrowserModule, 18 FormsModule, 19 ReactiveFormsModule, 20 NgBootstrapFormValidationModule.forRoot(), 21 HttpClientModule 22 ], 23 providers: [{ 24 provide: CUSTOM_ERROR_MESSAGES, 25 useValue: CUSTOM_ERRORS, 26 multi: true 27 }], 28 bootstrap: [AppComponent] 29}) 30export class AppModule { 31}
In addition to providing custom errors at the top level using the .forRoot()
method,
you can provide custom error messages to a specific control by binding to the
customErrorMessages
directive on the .form-group
element. Modifying the basic
example above, we can provide a one time custom error message to a specific .form-group
. Unlike the global custom error messages, these functions do not need to be individually exported.
custom-error-example.component.ts
1import {Component, OnInit} from "@angular/core"; 2import {FormControl, FormGroup, Validators} from "@angular/forms"; 3import {ErrorMessage} from "../../lib/Models/ErrorMessage"; 4 5@Component({ 6 selector: 'app-custom-errors', 7 templateUrl: './custom-errors.component.html', 8 styleUrls: ['./custom-errors.component.css'] 9}) 10export class CustomErrorsComponent implements OnInit { 11 12 formGroup: FormGroup; 13 14 customErrorMessages: ErrorMessage[] = [ 15 { 16 error: 'required', 17 format: (label, error) => `${label.toUpperCase()} IS DEFINITELY REQUIRED!` 18 }, { 19 error: 'pattern', 20 format: (label, error) => `${label.toUpperCase()} DOESN'T LOOK RIGHT...` 21 } 22 ]; 23 24 ngOnInit() { 25 this.formGroup = new FormGroup({ 26 Email: new FormControl('', [ 27 Validators.required, 28 Validators.pattern(/^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/) 29 ]), 30 Password: new FormControl('', [ 31 Validators.required, 32 Validators.minLength(8), 33 Validators.maxLength(20) 34 ]) 35 }); 36 } 37 38 onSubmit() { 39 console.log(this.formGroup); 40 } 41 42 onReset() { 43 this.formGroup.reset(); 44 } 45 46}
custom-error-example.component.html
1<div class="row"> 2 <div class="col-md-6 col-md-offset-3"> 3 <form [formGroup]="formGroup" (validSubmit)="onSubmit()"> 4 <div class="form-group" [customErrorMessages]="customErrorMessages"> 5 <label class="control-label">Email</label> 6 <input type="text" class="form-control" formControlName="Email"> 7 </div> 8 <div class="form-group"> 9 <label class="control-label">Password</label> 10 <input type="password" class="form-control" formControlName="Password"> 11 </div> 12 <button class="btn btn-default" type="button" (click)="onReset()">Reset</button> 13 <button class="btn btn-primary pull-right" type="submit">Submit</button> 14 </form> 15 </div> 16</div>
ng2-validation
validatorsNo vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 9/18 approved changesets -- score normalized to 5
Reason
project is archived
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
152 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More