Limit input input input box can only xxx

Limit input input input box can only xxx

  • Use onkeyup event, there are bug, that is, in the Chinese input method status, just press Enter after input Chinese characters, will directly enter the letters
  • Use onchange event, after typing in, lose focus when only input will get results, not just respond when input
  • Use oninput event, the perfect solution to these two problems, the test had no other problems.

Principle that will trigger events in the input event and will replace the regular expression will replace non-compliant (deleted) by character

Input uppercase and lowercase letters, numbers, underscores:

<input type="text" oninput="value=this.value.replace(/[^\w_]/g,'');"> 
复制代码

Enter lowercase letters, numbers, underscores:

<input type="text" oninput="value=this.value.replace(/[^a-z0-9_]/g,'');"> 
复制代码

Enter numbers and points

<input type="text" oninput="value=value.replace(/[^\d.]/g,'')">
复制代码

Input Chinese:

<input type="text" oninput="value=this.value.replace(/[^\u4e00-\u9fa5]/g,'')">  
复制代码

type in data:

<input type="text" oninput="value=this.value.replace(/\D/g,'')">  
复制代码

Enter English:

<input type="text" oninput="value=this.value.replace(/[^a-zA-Z]/g,'')">  
复制代码

Enter the Chinese, numbers, English:

<input oninput="value=value.replace(/[^\w\u4E00-\u9FA5]/g, '')">  
复制代码

Enter the numbers and letters:

<input oninput="value=value.replace(/[\W]/g,'')">  
复制代码

You can only enter letters and numbers, you can not enter Chinese

<input oninput="value=value.replace(/[^\w\.\/]/ig,'')">
复制代码

You can only enter numbers and

<input oninput="value=value.replace(/[^\d|chun]/g,'')">
复制代码

Reproduced in: https: //juejin.im/post/5d05e86b6fb9a07ef90c97e9

Guess you like

Origin blog.csdn.net/weixin_34179968/article/details/93176722