My goal is to validate a number
input field using Angular2:
<input type="number" class="form-control" name="foo" id="foo"
[min]="0" [max]="42" [(ngModel)]="foo" formControlName="foo">
In Chrome, everything works perfectly because it ignores characters as input, triggering the required validation when necessary. However, Firefox allows users to input characters into the field, leading to issues with the required validation. This results in problems like this:
https://i.stack.imgur.com/0oABN.png
The Issue
I want to be able to differentiate between empty values and invalid patterns. The only solution I have found so far is to change the input type to text
and use a pattern validator. This way, even if there are characters in the input, they won't trigger the validation for number
.
Is there a way to access the actual value of the field using the Angular2
FormGroup
object (even
returnsdocument.querySelector('#foo').value
null
despite the input showing something like42bar
)?Alternatively, is there a method to check if the input field contains characters?
Or should I stick to using
input type="text"
as the best option?