javascript - 使用 Angular Material 步进器在输入类型文本中添加自定义验证

标签 javascript angular validation angular-material angular-material-stepper

我正在使用 Angular 步进器来显示所有数据并为某些文本框获取输入,但我想在用户单击“下一步”按钮时添加自定义验证。

stackblitz - material-stepper-custom-validation

html:

<mat-horizontal-stepper #stepper>
  <mat-step>
    <div class="m-10">
      <input type="text" id="fname" placeholder="First Name" >
      </div>
      <div class="m-10">
      <input type="text" id="lname" placeholder="Last Name" >
      </div>
      <div>
        <button mat-button (click)="checkData()" matStepperNext>Next</button>
      </div>
  </mat-step>
  <mat-step [stepControl]="secondFormGroup" [optional]="isOptional">
        <button mat-button matStepperPrevious>Back</button>
        <button mat-button matStepperNext>Next</button>      
  </mat-step>
  <mat-step>
    <ng-template matStepLabel>Done</ng-template>
    You are now done.
    <div>
      <button mat-button matStepperPrevious>Back</button>
      <button mat-button (click)="stepper.reset()">Reset</button>
    </div>
  </mat-step>
</mat-horizontal-stepper>

此处,在进入下一个步进器之前应验证名字和姓氏,但不使用 formGroup。

ts:

import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
@Component({
  selector: 'stepper-optional-example',
  templateUrl: 'stepper-optional-example.html',
  styleUrls: ['stepper-optional-example.css']
})
export class StepperOptionalExample implements OnInit {

  constructor() {}

  ngOnInit() { }

  checkData() {    
    let lname = (<HTMLInputElement>document.getElementById("fname")).value;
    if(lname == '') {
      alert('error');
    }
    return false;
  }
}

如何? - 如果名字为空,则不允许他们进入下一步步进器。

最佳答案

因为您使用模板驱动方法,所以您需要以某种方式将所有输入字段映射到 ngModel 中。这是一个示例:

HTML:

<input type="text" required [(ngModel)]="model.name" name="name">

TS:

@Component({
  selector: 'stepper-optional-example',
  templateUrl: 'stepper-optional-example.html',
  styleUrls: ['stepper-optional-example.css']
})
export class StepperOptionalExample implements OnInit {

  @ViewChild('stepper') stepper;

  model = {
    name: 'Initial Value'
  }

  constructor() {}

  ngOnInit() { }

}

然后您可以使用它来检查 onClick 属性。您需要删除 matStepperNext 并添加 (click) 事件监听器,如下所示:

HTML:

<button mat-button (click)="onNext()">Next</button>

TS:

onNext() {
  // Validate your value in the function
  if (this.model.name !== 'Henry') {
    this.stepper.next();
  }
}

除此之外,我还建议您查看官方指南,了解如何实现模板驱动方法:https://angular.io/guide/forms

关于javascript - 使用 Angular Material 步进器在输入类型文本中添加自定义验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59244969/

相关文章:

javax 验证约束在 Spring Boot 中不起作用

javascript - Backbone.js 在路由器初始化函数中未定义

javascript - 使用 html5 导出按钮 meteor 表格导出数据

angular - ngx-mat-file-input mat-form-field 必须包含一个 MatFormFieldControl

javascript - Angular Highcharts hideNoData 不能动态工作

ruby-on-rails - Ruby on Rails 表单中的 HTML5 'required' 验证

javascript - 在优先于图像的图像之上添加一个图层

javascript - pm2运行node.js,但是经常重启:exited with code [0] via signal [SIGINT]

angular - 如何编写 Angular2 npm 模块 - Angular2 RC 6

c# - 如何防止使用属性验证器执行 C# 方法?