ade ramdani
ade ramdani

Reputation: 363

Filtering input fields in reactjs

Is there a way to block a specific character and only allow string from a - z in react? what I want is the user can only input a-z, and block others like [@,.-_] or even ' '(space).

Upvotes: 1

Views: 164

Answers (2)

ZaO Lover
ZaO Lover

Reputation: 433

We can use regx for filtering only alphanumeric values.

onChangeAlphaNumericInput(e) {
  const value = e.target.value;
  const regex = /[^A-Za-z]/ig; //this will admit letters, numbers and dashes
  if (value.match(regex) || value === "") {
    this.setState({ inputValue: value });
  }
}

Upvotes: 0

ramlalsharma1994
ramlalsharma1994

Reputation: 158

onChange={(e) => {
  let val = e.target.value

  val = val.replace(/[^A-Za-z]/ig, '')

  this.setState({
    value: val,
  })
}}

Upvotes: 2

Related Questions