Is there any way to restrict characters in ionic input type=text or number


#1

hello,

i’m using ionic 2 framework.is there any possible way to restrict the input type to certain characters.

i know minlength or maxlangth attributes work but these are accepting any characters shows red lines in the bottom. i what that if my input type minlength=1 and maxlength =1 then text field take only one character.


#2

There are two possible soultions that I can think of:


#3

Try:

  public onKeyUp(event: any) {

    let newValue = event.target.value;

    let regExp = new RegExp('^[A-Za-z0-9? ]+$');

    if (! regExp.test(newValue)) {
      event.target.value = newValue.slice(0, -1);
    }
  }

See: