Examples of Javascript implementations that prohibit typing in Chinese or English

  • 2020-03-30 04:32:44
  • OfStack

This tutorial shows you how to use Javascript to restrict input into English or Chinese.

English:


<input type="text" onkeypress="return event.keyCode>=48&&event.keyCode<=57||(this.value.indexOf('.')<0?event.keyCode==46:false)" onpaste="return !clipboardData.getData('text').match(/D/)" ondragenter="return false">

Limit Chinese input with regular expression:


<input type="text" onkeyup="value=value.replace(/[^u4E00-u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^u4E00-u9FA5]/g,''))" />

1. Use regular expressions to limit full-angle characters:


<input type="text" onkeyup="value=value.replace(/[^uFF00-uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^uFF00-uFFFF]/g,''))" />

2. Limit input to Numbers with regular expressions:


<input type="text" onkeyup="value=value.replace(/[^d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^d]/g,''))" />

3. Use regular expressions to restrict input to Numbers and English:


<input type="text" onkeyup="value=value.replace(/[W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^d]/g,''))" />


Related articles: