How to use min, max validation in template form angular 2 [duplicate]

人盡茶涼 提交于 2019-12-24 11:35:49

问题


I tried to use min validation in template form, but it didn't work. How to use it in template form? Thanks for your help.

      <input type="number" class="form-control" name="srvc_sub_cnt{{i}}" 
          [ngModel]="s?.srvc_sub_cnt" (ngModelChange)="s?.srvc_sub_cnt?
               s.srvc_sub_cnt=$event:null"
          required #srvc_sub_cnt="ngModel" pattern="^[0-9]+$" min="1">

        <div class="cell table-info" *ngIf="srvc_sub_cnt.errors?.min" class="form_error">
                    Service Subscribers must be greater than 0.
                  </div>

回答1:


To use min/max validations on input of type number you'll have to create Custom Validators

You can use this library. It implements a lot of custom validator

Including above library in your code, you can use min/max like

<input type="number" [(ngModel)]="model.field" name="field" #field="ngModel" [min]="10"/>
<p *ngIf="field.errors?.min">error message</p>


来源:https://stackoverflow.com/questions/46961901/how-to-use-min-max-validation-in-template-form-angular-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!