2

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>
TinaZ
  • 49
  • 1
  • 6
  • wasn't my answer helpful ? :p – Muhammad Usman Oct 26 '17 at 19:25
  • The easiest way could be *ngIf="s?.srvc_sub_cnt<1" – Vega Oct 26 '17 at 20:53
  • marked as duplicate, ping me if you disagree, but the dupe gives you the information you need. Even though that is talking about angular 2 final, things have not changed, in template driven form min/max does not work. Like in dupe, now min/max is supported in reactive forms, so you can consider going that route :) – AT82 Oct 28 '17 at 09:34
  • Thank you, AJT. I found the link you provided very useful. – TinaZ Oct 30 '17 at 19:46

1 Answers1

5

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>
Muhammad Usman
  • 10,039
  • 22
  • 39