Home >Web Front-end >JS Tutorial >JavaScript implements a method to control that only Chinese, numbers and English can be entered in the input box based on regular rules
This article mainly introduces JavaScriptOnly Chinese and Chinese characters can be entered in the control input box. The method of numbers and English implements the character input restriction function based on regular verification, which has certain reference value. Friends who need it can refer to it
The example of this article describes that only Chinese, numbers and English can be input in the Javascript control input box The method is shared with everyone for your reference. The details are as follows:
1. Problem background:
Encountered such a problem: There is an input box that requires only Enter Chinese, numbers and English (that is, filter special characters strings)
2. JS code:
function checkUsername() { //正则表达式 var reg = new RegExp("^[A-Za-z0-9\u4e00-\u9fa5]+$"); //获取输入框中的值 var username = document.getElementById("username").value.trim(); //判断输入框中有内容 if(!reg.test(username)) { alert("请输入中文、数字和英文!"); //输入非法字符,清空输入框 $("#username").val(""); } }
3. Page HTML Code:
<input type="text" id="username" onblur="checkUsername()"/>
The complete example code is as follows:
Test <input type="text" id="username" onblur="checkUsername()"/>
The running effect diagram is as follows:
The above is the detailed content of JavaScript implements a method to control that only Chinese, numbers and English can be entered in the input box based on regular rules. For more information, please follow other related articles on the PHP Chinese website!