0

I have this Angular 2 form:

<input #myInput="ngModel" [(ngModel)]="myobj.myInput" required min="0" type="number" /> 

<p>valid: {{myInput.valid}}</p>

When I enter "10" it is valid and when I enter "" it's invalid, but when I put "-10" it is still valid.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
balicekt
  • 615
  • 5
  • 14

1 Answers1

0

Because min isn't an option for FormsModule validation. The available validators are required, minlength, maxlength and pattern; an empty input is invalid because you have required. The min attribute prevents the HTML input from decrementing below zero using the arrow keys or the up-down buttons in the input, but isn't used for validation; you could write a custom validator to do this.

Note that there is an issue on GitHub discussing support for the HTML 5 validators; the fact that it's currently closed doesn't bode well for min and max getting included in the near future.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437