• 7.中文输入验证-原生JS


    
    
     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>
  • 相关阅读:
    vs2013+opencv2410的一些问题
    windows下bat批量处理启动exe
    https://blog.csdn.net/u012235003/article/details/54576737
    error LNK2005:"private:__thiscall编译错误
    后缀自动机学习笔记
    后缀数组学习笔记
    AC自动机学习笔记
    KMP
    Manacher学习笔记
    字符串Hash/树Hash学习笔记
  • 原文地址:https://www.cnblogs.com/mx2036/p/6704307.html
Copyright © 2020-2023  润新知