Reputation: 363
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
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
Reputation: 158
onChange={(e) => {
let val = e.target.value
val = val.replace(/[^A-Za-z]/ig, '')
this.setState({
value: val,
})
}}
Upvotes: 2