zoukankan      html  css  js  c++  java
  • js字符串判断

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>计算字符-Demo1</title>
    </head>
    <body>
    <div id="tips">最多10个字</div>
    <input type="text" value="" onkeyup="test(this)" />
    <script type="text/javascript">
    var num=15;
         function len(x){
         var text=x;
        return text.replace(/[^x00-xff]/g,"**").length;        //汉字替换为2字符
        }
    function test(obj){
    var tips = document.getElementById("tips");
    var str = obj.value;
    var n=Math.ceil(len(str)/2);
        if((num-n)>0){
        tips.innerHTML="你还可以输入"+(num-n)+"个字";
        }else{
        thips.innerHTML="你还可以输入"+(n-num)+"个字";
        }
        
    }
    </script>
    </body>
    </html>

    写法2:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>计算字符-Demo1</title>
    </head>
    <body>
    <div id="tips">最多10个字</div>
    <input type="text" value="" onkeyup="test(this)" />
    <script type="text/javascript">
    var num=15;
         String.prototype.len=function(){
        return this.replace(/[^x00-xff]/g,"**").length;        //汉字替换为2字符
        }
    function test(obj){
    var tips = document.getElementById("tips");
    var str = obj.value;
    var n=Math.ceil(str.len()/2);
        if((num-n)>0){
        tips.innerHTML="你还可以输入"+(num-n)+"个字";
        }else{
        thips.innerHTML="你还可以输入"+(n-num)+"个字";
        }
        
    }
    </script>
    </body>
    </html>
  • 相关阅读:
    8.2.8 A Simple Game
    8.2.7 Be the Winner
    8.2.6 John
    8.2.5 kiki's game
    8.2.2 Good Luck in CET-4 Everybody!
    8.2.4 悼念512汶川大地震遇难同胞——选拔志愿者
    8.1.8 Team Queue
    8.2.1 Brave Game
    8.1.7 愚人节的礼物
    8.1.6 Windows Message Queue
  • 原文地址:https://www.cnblogs.com/tinyphp/p/3374630.html
Copyright © 2011-2022 走看看