How to prevent special characters to be entered in ion-input field


#1

I am using Ionic 3.
I want an ion-input field which only accepts alphanumeric chars. So if user presses any other char from soft keypad it will not be shown in input field. Input field should reject any such char entry.
I used keypress event with event.preventDefault but it doesn’t work.
Give some direction.


#2

why not bind some function on ionChange which checks if the string associated with input has any special character or not and if yes it removes it from the string? So when any change to input is made, special characters are checked for and if found, are removed.


#3

^ this.

Just use a simple regular expression to check for the input and if one is found, remove it from the string.
If you have no clue about regular expressions, worry not, take a look at this StackOverflow post which contains everything you need => https://stackoverflow.com/questions/388996/regex-for-javascript-to-allow-only-alphanumeric