• 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>
  • 相关阅读:
    jconsole远程连接监控tomcat
    Nginx监控配置
    Nginx安装+2tomcat配置
    Flexbox布局模式的理解
    Web性能优化:图片优化
    大公司里怎样开发和部署前端代码?
    原生js下拉刷新
    全国三级城市联动 js版
    js获取智能机浏览器版本信息
    JS年月日三级联动下拉框日期选择代码
  • 原文地址:https://www.cnblogs.com/mx2036/p/6704307.html
Copyright © 2020-2023  润新知