0

I have a one input field and it will accept 8 numbers with dot and i have to restrict special character under that. Please provide regexp or any other way to achieve this. I am checking below code on keypress:

isNumberKey(evt, val) {
const charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode !== 8) {
  if (val.length !== undefined) {
    if (val.length > 7) {
      return false;
    }
  }
  if (charCode === 190 || charCode === 110) {
    if (!val.includes('.')) {
      this.keyInputFlag = true;
      return true;
    }
    return false;
  } else if (charCode >= 48 && charCode <= 57
    || (charCode >= 96 && charCode <= 105)) {
    this.keyInputFlag = true;
    return true;
  }
  return false;
}
if (charCode === 8) {
  this.keyInputFlag = true;
  return true;
}
return false;

}

6
  • 2
    Could show you code and what are kind of special character need be restrict?
    – verejava
    Jun 3, 2019 at 3:46
  • Need to block (!@#$%^&*()_+*/><~) this special characters. Jun 3, 2019 at 3:52
  • There are some plugins for this, in case you are interested, an example could be imaskjs
    – Shidersz
    Jun 3, 2019 at 3:53
  • regexp : /^[\d\.]+$/ Numbers and dots
    – verejava
    Jun 3, 2019 at 4:09
  • @verejava I have updated my comment. Jun 3, 2019 at 5:15

0

Browse other questions tagged or ask your own question.