Home > Web Front-end > JS Tutorial > body text

JavaScript implements a method to control that only Chinese, numbers and English can be entered in the input box based on regular rules

黄舟
Release: 2017-03-23 14:56:24
Original
3104 people have browsed it

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("");
 }
}
Copy after login

3. Page HTML Code:

<input type="text" id="username" onblur="checkUsername()"/>
Copy after login

The complete example code is as follows:





Test




<input type="text" id="username" onblur="checkUsername()"/>

Copy after login

The running effect diagram is as follows:

JavaScript implements a method to control that only Chinese, numbers and English can be entered in the input box based on regular rules

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template