1 <!DOCTYPE html>
2 <html>
3 <head lang="en">
4 <meta charset="UTF-8">
5 <title>中文输入验证-原生JS</title>
6 </head>
7 <body>
8 <input type="text" id="num" onblur="an()" onfocus="bn()"/>
9
10 <p id="p"></p>
11
12 </body>
13
14 <script type="text/javascript">
15 var an = function () {
16 var reg = /^[u4e00-u9fa5]*$/;
17 //var reg=/^-?d+$/;//整数
18 // var reg=/^d+$/;//正整数
19
20 var input1 = document.getElementById("num").value;
21 var p1 = document.getElementById("p").innerHTML;
22
23 // console.log("正则:;"+reg.test(str));
24 if (!reg.test(input1)) {
25 document.getElementById("p").innerHTML = "请输入中文汉字";
26 document.getElementById("num").value = "";
27 } else {
28
29 document.getElementById("p").innerHTML = "输入合法";
30 }
31
32 };
33
34 var bn = function () {
35 document.getElementById("p").innerHTML = "";
36 }
37
38
39 </script>
40
41 </html>
View Code
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>中文输入验证-原生JS</title>
</head>
<body>
<input type="text" id="num" onblur="an()" onfocus="bn()"/>
<p id="p" ></p>
</body>
<script type="text/javascript">
var an = function () {
var reg = /^[u4e00-u9fa5]*$/;
// var reg=/^-?d+$/;//整数
// var reg=/^d+$/;//正整数
var input1 = document.getElementById("num").value;
var p1 = document.getElementById("p").innerHTML;
// console.log("正则:;"+reg.test(str));
if (!reg.test(input1)) {
document.getElementById("p").innerHTML = "请输入中文汉字";
document.getElementById("num").value = "";
} else {
document.getElementById("p").innerHTML = "输入合法";
}
};
var bn = function () {
document.getElementById("p").innerHTML = "";
}
</script>
</html>