In Angular2, how can I mask an input field(textbox) such that it accepts only numbers and not alphabets?
I have the following HTML input:
<input type="text" *ngSwitchDefault class="form-control" (change)="onInputChange()" [(ngModel)]="config.Value" focus)="handleFocus($event)" (blur)="handleBlur($event)"/>
The above input is a generic text input which may either be used as a simple text field or as a numeric field, (for example to show the Year).
Using angular2, how can I use the same input control and apply some sort of filter/mask on this field, such that it accepts only numbers? What are the different ways I can achieve this?
Note: I need to achieve this using only textbox and not using input number type.
You need to use type="number" instead text. You can also specify max and min numbers
fromCharCode returns 'a' when pressing on the numpad '1' so this methoid should be avoided
(admin: could not comment as usual)
you can achive it like this
hope this will help you.
A more concise solution. Try this directive.
Can also be used if you're using ReactiveForms.
The use it on your inputs like this:
Just use HTML5, input type=”number”