@angular-material-extensions/password-strength

中文文档
16.0.0发布于 2023/11/05MITTypeScriptES Module近 30 天下载 3.1万

Material password strength meter to indicate how secure is the provided password

原始使用文档
文档目录28 节
  1. Built by and for developers :heart:
  2. Table of Contents
  3. Demo
  4. Library's components
  5. Dependencies
  6. Installation
  7. 1. Install via ng add . (Recommended)
  8. 2. Install via npm . (Alternative)
  9. Import the library
  10. API
  11. <mat-password-strength> used to calculate and display the s…
  12. <mat-password-strength-info> used to display more informati…
  13. <mat-pass-toggle-visibility> used to show/hide the password…
  14. Usage
  15. In the following example, we integration a material input c…
  16. Example of how to use the emitted strength of the password …
  17. Use the toggle visibility component
  18. Client Side password's validation using a built in angular …
  19. custom regex validation
  20. Confirm the password with built in angular form controllers…
  21. Use always the green color for a strong password just by ad…
  22. Supporting custom messages and ngx-translate for the info c…
  23. Run Demo App Locally
  24. Development
  25. Other Angular Libraries
  26. Support
  27. Who is using this library? Awesome apps?
  28. License

angular-material-extensions's logo

@angular-material-extensions/password-strength - Material password strength meter to indicate how secure is the provided password

npm version npm demo docs: typedoc Join the chat at https://gitter.im/angular-material-extensions/Lobby CircleCI branch Build Status codecov dependency Status devDependency Status Greenkeeper Badge license Awesome

@angular-material-extensions/password-strength demonstration

Built by and for developers :heart:

Do you have any question or suggestion ? Please do not hesitate to contact us! Alternatively, provide a PR | open an appropriate issue here

If you like this project, support angular-material-extensions by starring :star: and sharing it :loudspeaker:

Table of Contents

Demo

View all the directives and components in action at https://angular-material-extensions.github.io/password-strength

Library's components

  • <mat-password-strength> used to calculate and display the strength of a provided password
  1. strength score <= 20%

@angular-material-extensions/password-strength score less than 20%

  1. strength score <= 80%

@angular-material-extensions/password-strength score less than 40%

  1. strength score > 80%

@angular-material-extensions/password-strength score less than 100%

  • <mat-password-strength-info> used to display more information about the strength of a provided password

@angular-material-extensions/password-strength's info

  • <mat-pass-toggle-visibility> used to show/hide the password provided in the input element

@angular-material-extensions/password-strength's info


Dependencies

  • Angular developed and tested with 15.x

Installation

If Angular Material Design is not setup, just run ng add @angular/material learn more

Now add the library via the angular schematics

ng add @angular-material-extensions/password-strength

2. Install via npm. (Alternative)

Now install @angular-material-extensions/password-strength via:

npm install --save @angular-material-extensions/password-strength

SystemJS

Note:If you are using SystemJS, you should adjust your configuration to point to the UMD bundle. In your systemjs config file, map needs to tell the System loader where to look for @angular-material-extensions/password-strength:

{
  '@angular-material-extensions/password-strength';: 'node_modules/@angular-material-extensions/password-strength/bundles/password-strength.umd.js',
}

-> follow the instructions here


Import the library

Once installed you need to import the main module:

import { MatPasswordStrengthModule } from "@angular-material-extensions/password-strength";

The only remaining part is to list the imported module in your application module. The exact method will be slightly different for the root (top-level) module for which you should end up with the code similar to (notice MatPasswordStrengthModule .forRoot()):

import { MatPasswordStrengthModule } from '@angular-material-extensions/password-strength';

@NgModule({
  declarations: [AppComponent, ...],
  imports: [MatPasswordStrengthModule.forRoot(), ...],
  bootstrap: [AppComponent]
})
export class AppModule {
}

Other modules in your application can simply import MatPasswordStrengthModule:

import { MatPasswordStrengthModule } from '@angular-material-extensions/password-strength';

@NgModule({
  declarations: [OtherComponent, ...],
  imports: [MatPasswordStrengthModule, ...],
})
export class OtherModule {
}

API

<mat-password-strength> used to calculate and display the strength of a provided password - see the demo examples

optionbindtypedefaultdescription
passwordInput() string-the password to calculate its strength
customValidatorInput() RegExp-custom regex validator
externalErrorInput() booleanfalseused to change the color of the password to warn if an external error occurs
enableLengthRuleInput() booleantruewhether to validate the length of the password
enableLowerCaseLetterRuleInput() booleantruewhether a lowercase letter is optional
enableUpperCaseLetterRuleInput() booleantruewhether a uppercase letter is optional
enableDigitRuleInput() booleantruewhether a digit char is optional
enableSpecialCharRuleInput() booleantruewhether a special char is optional
minInput() number8the minimum length of the password
maxInput() number30the maximum length of the password
warnThresholdInput() number21password strength less than this number shows the warn color
accentThresholdInput() number81password strength less than this number shows the accent color
onStrengthChangedOutput()number-emits the strength of the provided password in % e.g: 20%, 40%, 60%, 80% or 100%

<mat-password-strength-info> used to display more information about the strength of a provided password - see the demo examples

optionbindtypedefaultdescription
passwordComponentInput() PasswordStrengthComponent-the password component used in the template in order to display more info related to the provided password
enableScoreInfoInput() booleanfalsewhether to show the password's score in %
lowerCaseCriteriaMsgInput() stringcontains at least one lower characteran appropriate msg for the lower case %
upperCaseCriteriaMsgInput() stringcontains at least one upper characteran appropriate msg for the upper case %
digitsCriteriaMsgInput() stringcontains at least one digit characteran appropriate msg for the digit case %
specialCharsCriteriaMsgInput() stringcontains at least one special characteran appropriate msg for the special case %
customCharsCriteriaMsgInput() stringcontains at least one custom characteran appropriate msg for the custom validator case %
minCharsCriteriaMsgInput() stringcontains at least ${this.passwordComponent.min} charactersan appropriate msg for the minimum number of chars %

<mat-pass-toggle-visibility> used to show/hide the password provided in the input element

optionbindtypedefaultdescription
isVisibleInput() booleanfalsewhether the password is visible or hidden
tabindexInput() stringnullset the desired tabindex value of the toggle visibility button.

Usage

add the @angular-material-extensions/password-strength element to your template:

<mat-password-strength [password]="password.value"> </mat-password-strength>

This will display only the material password strength meter in form of a progress without any input fields or similar.

In the following example, we integration a material input container with @angular-material-extensions/password-strength 's component.

NOTE: In order to repaint the mat-form-field correctly after changing the value of the password's strength, please consider to change the detection strategy for the parent component -->

import {
  ChangeDetectionStrategy,
  Component,
  OnInit,
  ViewEncapsulation,
} from "@angular/core";
import { Title } from "@angular/platform-browser";
import { MatSlideToggleChange } from "@angular/material";
import { MatPasswordStrengthComponent } from "@angular-material-extensions/password-strength";

@Component({
  selector: "app-home",
  templateUrl: "./home.component.html",
  styleUrls: ["./home.component.scss"],
  encapsulation: ViewEncapsulation.None,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class HomeComponent implements OnInit {}
<div>
  <mat-form-field
    appearance="outline"
    style="width: 100%"
    [color]="passwordComponent.color"
  >
    <mat-label>Password</mat-label>
    <input
      matInput
      #password
      [type]="inputType"
      required
      placeholder="Password"
    />
    <mat-hint align="end" aria-live="polite">
      {{password.value.length}} / 25
    </mat-hint>
  </mat-form-field>

  <mat-password-strength
    #passwordComponent
    (onStrengthChanged)="onStrengthChanged($event)"
    [password]="password.value"
  >
  </mat-password-strength>
</div>

learn more about mat-form-field

Example of how to use the emitted strength of the password in your template

<div fxLayout="row" fxLayoutGap="10px">
  <div *ngIf="passwordComponent.strength === 100; then done else error"></div>
  <ng-template #done>
    <mat-icon color="primary">done</mat-icon>
  </ng-template>
  <ng-template #error>
    <mat-icon color="warn">error</mat-icon>
  </ng-template>
  <div>
    <p>Password's strength = {{passwordComponent.strength}} %100</p>
  </div>
</div>

Use the toggle visibility component

  • add the mat-pass-toggle-visibility to your mat-form-field
  • give it a name to use it in the html file like toggle
  • set the type of the input to that value emitted from the mat-pass-toggle-visibility component <input matInput [type]="toggle.type"/>
<mat-form-field
  appearance="outline"
  style="width: 100%"
  [color]="passwordComponent.color"
>
  <mat-label>Password</mat-label>
  <!-- HERE DOWN :D-->
  <mat-pass-toggle-visibility #toggle matSuffix></mat-pass-toggle-visibility>
  <!-- THERE ABOVE ;)-->
  <input
    matInput
    #password
    [type]="toggle.type"
    required
    placeholder="Password"
  />
  <mat-hint align="end" aria-live="polite">
    {{password.value.length}} / 25
  </mat-hint>
</mat-form-field>

Client Side password's validation using a built in angular formController

  1. add an input element to your template with an appropriate @angular-material-extensions/password-strength's component
  2. hold a reference of the @angular-material-extensions/password-strength's component by adding passwordComponentWithValidation (or whatever you want) inside the element

e.g:

<mat-password-strength
  #passwordComponentWithValidation
  [password]="passwordWithValidation.value"
>
</mat-password-strength>
  1. bind the form controller of the mat-password-strength to the input element
  • you can access the form controller of @angular-material-extensions/password-strength using the chile view --> passwordComponentWithValidation.passwordFormControl
  • bind the form controller to an input element --> [formControl]="passwordComponentWithValidation.passwordFormControl"
  1. Full example - see below
<div>
  <mat-form-field appearance="outline" style="width: 100%">
    <mat-label>Password</mat-label>
    <input
      matInput
      #passwordWithValidation
      [type]="inputType"
      required
      [formControl]="passwordComponentWithValidation.passwordFormControl"
      placeholder="Password"
    />
    <mat-hint align="end" aria-live="polite">
      {{passwordWithValidation.value.length}} / 25
    </mat-hint>
    <mat-error
      *ngIf="passwordComponentWithValidation.passwordFormControl.hasError('required')"
    >
      Password is required
    </mat-error>
    <mat-error
      *ngIf="passwordComponentWithValidation.passwordFormControl.hasError('pattern')"
    >
      Password is not valid
    </mat-error>
  </mat-form-field>
  <mat-password-strength
    #passwordComponentWithValidation
    (onStrengthChanged)="onStrengthChanged($event)"
    [password]="passwordWithValidation.value"
  >
  </mat-password-strength>
  <!--Password's strength info-->
  <mat-password-strength-info
    [passwordComponent]="passwordComponentWithValidation"
  >
  </mat-password-strength-info>
</div>

this will looks like -->

@angular-material-extensions/password-strength


custom regex validation

please consider to use the customValidator input (see below)

<mat-slide-toggle #toggle>Show Password Details</mat-slide-toggle>

<mat-form-field
  appearance="outline"
  style="width: 100%"
  [color]="passwordComponent.color"
>
  <mat-label>Password</mat-label>
  <mat-pass-toggle-visibility
    #toggleVisbility
    matSuffix
  ></mat-pass-toggle-visibility>
  <input
    matInput
    #password
    [type]="toggleVisbility.type"
    placeholder="Password"
  />
  <mat-hint align="end" aria-live="polite">
    {{password.value.length}} / {{passwordComponent.max}}
  </mat-hint>
</mat-form-field>

<mat-password-strength
  #passwordComponent
  (onStrengthChanged)="onStrengthChanged($event)"
  [password]="password.value"
  [customValidator]="pattern"
>
</mat-password-strength>

<mat-password-strength-info
  *ngIf="toggle.checked"
  [passwordComponent]="passwordComponent6"
  customCharsCriteriaMsg="1 german special chars is required"
  [enableScoreInfo]="true"
>
</mat-password-strength-info>
pattern = new RegExp(/^(?=.*?[äöüÄÖÜß])/);

Confirm the password with built in angular form controllers - see the live example

@angular-material-extensions/password-strength with confirmation feature

@angular-material-extensions/password-strength with confirmation feature

Use always the green color for a strong password just by adding the green css class to the mat-password-strength

<mat-password-strength
  #passwordComponent
  class="green"
  [password]="password.value"
>
</mat-password-strength>

Supporting custom messages and ngx-translate for the info component please check the example demo here

@angular-material-extensions/password-strength demonstration

for more examples please visit this URL : [(https://angular-material-extensions.github.io/password-strength/examples]((https://angular-material-extensions.github.io/password-strength/examples)

Please checkout the full documentation here or follow the official tutorial


Run Demo App Locally

Development

Other Angular Libraries


Support

Built by and for developers :heart: we will help you :punch:


Who is using this library? Awesome apps?

  1. Nahaus.de - Digitale und automatisierte Immobilienverwaltung Software für privat Vermieter und Hausverwaltungen

Are you missing your project or you app? PR me to publish it on the README



jetbrains logo

This project is supported by jetbrains with 1 ALL PRODUCTS PACK OS LICENSE incl. webstorm


License

Copyright (c) 2019-2023 Anthony Nahas. Licensed under the MIT License (MIT)

版本与发布记录

@angular-material-extensions/password-strength 共发布 44 个版本,首次发布于 2018/06/19;过去 12 个月发布了 0 个版本。

版本发布日期距今
16.0.0latest2023/11/053 年前
13.0.02023/08/263 年前
12.1.02023/02/223 年前
12.0.22022/11/263 年前
12.0.12022/11/263 年前
11.0.12022/10/154 年前

关于 @angular-material-extensions/password-strength 的常见问题

@angular-material-extensions/password-strength 是什么?

Material password strength meter to indicate how secure is the provided password

如何安装 @angular-material-extensions/password-strength?

在项目目录运行 npm install @angular-material-extensions/password-strength,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@angular/common(^16.x)、@angular/core(^16.x)、@angular/forms(^16.x)、@angular/cdk(^16.x)、@angular/material(^16.x)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@angular-material-extensions/password-strength 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@angular-material-extensions/password-strength 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@angular-material-extensions/password-strength 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=14。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@angular-material-extensions/password-strength 可以商用吗?

@angular-material-extensions/password-strength 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@angular-material-extensions/password-strength 的下载量有多少?

根据 npm 官方下载统计,@angular-material-extensions/password-strength 在 2026/10/06 – 2026/11/04 这 30 天内被下载 31,320 次,日均约 1,044 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@angular-material-extensions/password-strength 还在维护吗?

最新版本 16.0.0 发布于 2023/11/05(3 年前);至今共发布 44 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/06/19。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。