How to allow only English letters in input fields?

前端 未结 4 1647
无人及你
无人及你 2021-01-14 17:30

So, this is my input field:


How can I allow only English letters?

This is the RegEx

4条回答
  •  滥情空心
    2021-01-14 17:49

    I would try this onChange function:

    onChange={(e) => {
      let value = e.target.value
    
      value = value.replace(/[^A-Za-z]/ig, '')
    
      this.setState({
        value,
      })
    }}
    

    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 ''

提交回复
热议问题