Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Min and max value of input in angular4 application

I have an angular4 application with a form. In this one I have an input to enter a percentage. So, I want to block the input with value between 0 and 100. I tried to add min="0" and max="100" but I can yet enter an number higher than 100 or smaller than 0.

template

<md-input-container>   <input type="number"      maxlength="3"      min="0"      max="100"      required      mdInput      placeholder="Charge"      [(ngModel)]="rateInput"      name="rateInput">   <md-error>Required field</md-error> </md-input-container> 

Do you know how I can do this ?

like image 524
Adrien Avatar asked Aug 03 '17 09:08

Adrien


People also ask

How do you set the minimum and maximum value of an input?

The min attribute specifies the minimum value for an <input> element. Tip: Use the min attribute together with the max attribute to create a range of legal values. Note: The max and min attributes works with the following input types: number, range, date, datetime-local, month, time and week.

Which attributes are used to specify the maximum and minimum value for an input field?

min attribute specifies a minimum value and max attributes specify a maximum value for the input field.

What is min and max attribute?

In HTML, the min and max attributes are used to set the range of values for the <input> element. The min attribute sets the minimum value for the <input> element, while the max attribute sets the maximum value for the <input> element. We can use the min/max attribute with the following data types: Data.


1 Answers

I succeeded by using a form control. This is my html code :

<md-input-container>     <input type="number" min="0" max="100" required mdInput placeholder="Charge" [(ngModel)]="rateInput" name="rateInput" [formControl]="rateControl">     <md-error>Please enter a value between 0 and 100</md-error> </md-input-container> 

And in my Typescript code, I have :

this.rateControl = new FormControl("", [Validators.max(100), Validators.min(0)]) 

So, if we enter a value higher than 100 or smaller than 0, the material design input become red and the field is not validate. So after, if the value is not good, I don't save when I click on the save button.

like image 62
Adrien Avatar answered Sep 29 '22 15:09

Adrien