I'm trying to make the button only available when a certain range of values is put into a separate input but it stays disabled even if the correct condition is met.
The idea is to stop the user from clicking the button in case he puts incorrect data in the input and goes directly to the button - in this situation, he won't be prompted that he should correct the number of passengers.
HTML:
<input type="number" id="passengers" [(ngModel)]="numberOfPassengers" (change)="checkValue()">
<a routerLink="/summary"> <button [disabled]="checkButton()" (click)="saving()">To summary</button></a>
COMPONENT.TS
ngOnInit() {
this.buttonDisabled = true
}
public numberOfPassengers: number;
public errorMessage: string = "";
public errorMessageSubmit: string = ""
public buttonDisabled: boolean;
checkButton() {
if (this.numberOfPassengers <= 0 && this.numberOfPassengers > 9) {
return true;
} else {
return false;
}
}
checkValue() {
if (this.numberOfPassengers <= 0) {
this.errorMessage = "Choose at least 1 passenger";
return (this.numberOfPassengers = 1);
} else if (this.numberOfPassengers > 9) {
this.errorMessage = "Maximum of 9 passengers is allowed";
return (this.numberOfPassengers = 9);
}
}
What am I doing wrong? Thanks for any suggestions!