So, this is my input field:
JavaScript
x
2
1
<input type={type} name={name} />
2
How can I allow only English letters?
This is the RegEx
, I believe I should use: /[A-Za-z]/ig
https://regex101.com/r/upWFNy/1
I am assuming that onChange()
event should be used for this with the combination of setState()
and event.target.value
.
Thanks.
PS. I need to have this WHILE typing.
Advertisement
Answer
I would try this onChange
function:
JavaScript
1
10
10
1
onChange={(e) => {
2
let value = e.target.value
3
4
value = value.replace(/[^A-Za-z]/ig, '')
5
6
this.setState({
7
value,
8
})
9
}}
10
See the codepen: https://codepen.io/bozdoz/pen/vzJgQB
The idea is to reverse your regex matcher with ^
and replace all non-A-z characters with ''