Search code examples
angularangular2-templateangular-forms

Submit button becomes not disable but it doesn't have be disable when check on number format


I use angular and I use Form and in particular the Template driven form. In particular I have a number that can be major to zero and it must have two decimal places so:

<form (ngSubmit)="onSubmitForm()" #search="ngForm">
  <input type="text" ngModel class="form-control" name="search_Home" required min=0>
  <button class="btn btn-primary" type="submit" [disabled]="!search.valid">Submit</button>
</form>

The problem is that when I don't put anything the button is disabled but if I put the value "-3" the button became clickable but it is not correct.Anyone can help me?


Solution

  • HTML

    <form (ngSubmit)="onSubmitForm()" #search="ngForm">
      <input type="text" [ngModel]="searchHome" (input)="isNumber($event)" class="form-control" name="search_Home" required min=0>
      <button class="btn btn-primary" type="submit" [disabled]="isFormValid">Submit</button>
    </form>
    

    Component:

    isBtnSubmitDisabled = true;
    
    isNumber(value) {
      if(value && /* cond */) {
        this.isBtnSubmitDisabled = false;
      }
    }
    
    get isFormValid() {
      return this.isBtnSubmitDisabled;
    }