Partha Sarathi Ghosh
Partha Sarathi Ghosh

Reputation: 11596

How to get Error message when using pattern in angular 2

I have tried this code but it is not working.

<div>
    <span *ngIf="!usernameRef.errors?.required">Amount</span>
    <span *ngIf="!usernameRef.errors?.required">Cover amount required.</span>
    <span *ngIf="usernameRef.errors?.pattern">Cover amount invalid.</span>

     <input type="number" name="Amount" class="form-control" data-field="validate" placeholder="50 000" [(ngModel)]="Amount" 
    required #usernameRef="ngModel" pattern="[0-9]*" /> 
    </div>

Upvotes: 0

Views: 605

Answers (1)

Bazinga
Bazinga

Reputation: 11234

Your regex is also valid from two reasons:

  1. Your input type is a number. You can't type letters.
  2. [0-9]* - the asterisk means - 0 or more occurrences.

Upvotes: 1

Related Questions