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

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.

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.

^ 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 =>

1 Like

use this one

letterNumberOnly(name) {
    var re = /^[A-Za-z0-9' ]*$/;
    return re.test(String(name));
let booleanValue=this.letterNumberOnly('a2hi');
return true;
return false;