JavaScript implementations prohibit entering Chinese or English examples _javascript tips

Source: Internet
Author: User

This tutorial teaches you how to use JavaScript to restrict input to English, or to restrict the entry of Chinese.

No English input:

Copy Code code as follows:

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

Only Chinese can be entered with regular expression restrictions:

Copy Code code as follows:

<input type= "text" onkeyup= "Value=value.replace (/[^\u4e00-\u9fa5]/g, ')" Onbeforepaste= "Clipboarddata.setdata ( ' Text ', Clipboarddata.getdata (' text '). Replace (/[^\u4e00-\u9fa5]/g, ') "/>

1. Only full-width characters can be entered with regular expression restrictions:

Copy Code code as follows:

<input type= "text" onkeyup= "Value=value.replace (/[^\uff00-\uffff]/g, ')" Onbeforepaste= "Clipboarddata.setdata ( ' Text ', Clipboarddata.getdata (' text '). Replace (/[^\uff00-\uffff]/g, ') "/>

2. Only numbers can be entered with regular expression restrictions:

Copy Code code as follows:

<input type= "text" onkeyup= "Value=value.replace (/[^\d]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\d]/g, ') "/>

3. Only numbers and English can be entered with regular expression restrictions:

Copy Code code as follows:

<input type= "text" onkeyup= "Value=value.replace (/[\w]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\d]/g, ') "/>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.