1

In my angular 4 project I have multiple forms, some of these forms have an input field that needs to be controlled, and if the value isn't correct the button save needs to be disabled.

If i want a input field with only positive numbers I use this:

<input type="number" class="form-control" id="blockFrom" required  
[(ngModel)]="blockFrom" min="0" name="blockFrom">

<button (click)="save()" [disabled]="modelForm.form.invalid || 
modelForm.form.pristine">save</button>

But if I write in the input field negative numbers the button is enabled (I see the input field red correctly because the value isn't permitted) Why? How can I block the button if value is negative?

Alessandro Celeghin
  • 4,039
  • 14
  • 49
  • 95
  • Possible duplicate of [Min / Max Validator in Angular 2 Final](https://stackoverflow.com/questions/39847862/min-max-validator-in-angular-2-final) – Igor Aug 31 '17 at 13:31

1 Answers1

2

You can put a function directly here: [disabled]="checkIfValid()".

The function will then checks your form data.

it should look like this:

checkIfValid():boolean
{
 return blockForm>0
}
DavidPi
  • 415
  • 3
  • 18