rnadler / ng2-password-strength-bar

Angular 2/4/5 Password Strength Bar
http://bobonmedicaldevicesoftware.com/blog/2016/12/09/publishing-an-angular-2-component-npm-package/
MIT License
20 stars 17 forks source link

ng2-password-strength-bar

Build Status npm version

This an Angular 2 implementation of AngularJS Directive to test the strength of a password.

Try it live!

Angular 9 Support

If you are using Angular 9+ you will need to use ng9-password-strength-bar.

The only configuration differences between the ng2 and ng9 versions are these name changes:

Install in your project

npm install ng2-password-strength-bar --save

Version Update Note

If you upgrade from v1.0.x to v1.1.x (or greater) you will need to change the PasswordStrengthBar import. There are two options:

  1. Change to PasswordStrengthBarModule and add this to the imports section of the @NgModule metadata instead of the declarations (shown below), or
  2. Change to PasswordStrengthBarComponent and update the name in the declarations section to match.

Using the Component

Add Component to Module imports

import { PasswordStrengthBarModule } from 'ng2-password-strength-bar';
//...
@NgModule({
 //...
   declarations: [
        AppComponent,
        //...
    ],
    imports: [
      BrowserModule,
      FormsModule,
      PasswordStrengthBarModule,
      //...
 //...
})
export class AppModule {}

Add Component to your Application

@Component({
    selector: 'my-app',
    template: `
  <h3>Angular 2 Password Strength Bar</h3>
    <div>
       <form name="myForm" novalidate>
            <input type="password" class="form-control" id="password" name="password" placeholder="Enter password"
                 [(ngModel)]="account.password" #password="ngModel"
                 minlength="5" maxlength="50" required>
            <ng2-password-strength-bar
                [passwordToCheck]="account.password"
                [barLabel]="barLabel"
                [barColors]="myColors">
            </ng2-password-strength-bar>
        </form>
    </div>
  `,
})
export class App {
    public account = {
        password: <string>null
    };
    public barLabel: string = "Password strength:";
    public myColors = ['#DD2C00', '#FF6D00', '#FFD600', '#AEEA00', '#00C853'];
    // ...
}

Parameters

<ng2-password-strength-bar
  [passwordToCheck]="account.password"
  [barLabel]="barLabel"
  [barColors]="myColors"
  [baseColor]="baseColor"
  [strengthLabels]="strengthLabels"
  (onStrengthChanged)="strengthChanged($event)">
</ng2-password-strength-bar>

Input Parameters

passwordToCheck (type: string)

barLabel (type: string)

barColors (type: Array\<string>, optional)

(New in v1.2.0)

baseColor (type: string, optional)

(New in v1.2.1)

strengthLabels (type: Array\<string>, optional)

(New in v1.2.1)

Output Parameters

onStrengthChanged(strength: number) optional

Run the tests locally

License

MIT