js检测输入内容全为空格的方法


JavaScript判断输入内容不能全为空格的方法,用来避免输入框的内容输入的全是空格:
复制代码 代码如下:

<html>
 <head>
  <script>
   function checkBlankSpace(str){
    while(str.lastIndexOf(" ")>=0){
      str = str.replace(" ","");
    }
    if(str.length == 0){
     alert("输入不能全为空");
    }
   }
   function test(){
    var testStr = document.getElementById("test").value;
    checkBlankSpace(testStr);
   }
  </script>
 </head>
 <body>
  <input type="text" id="test"/>
  <input type="button" value="测试" onclick="test()">
 </body>
</html>

« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3