javascript limit text input characters

前端 未结 5 1280
攒了一身酷
攒了一身酷 2020-12-22 06:30

I am wanting to restrict the input characters for a text box to [a-z0-9_-]. However whenever if do this buttons like backspace and the arrow keys don\'t work. I have found s

5条回答
  •  渐次进展
    2020-12-22 07:09

    You haven't provided any code samples, so it's hard to be specific in a response, but as a general strategy, try this: instead of trying to whitelist characters that can be input while they are being typed in, validate the contents of the text box after every key stroke to make sure that it still contains valid characters. If it doesn't, remove the last character entered.

    This approach will allow special keys like backspace, etc., while at the same time achieve what it sounds like you are really after: a valid value in the text box.

提交回复
热议问题