<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>判断输入纯空格</title> <script src="http://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script> </head> <body> <input type="text" class="put" /> <button class="btn">点击我</button> </body> <script> var oPut = document.querySelector(".put"); var oBtn = document.querySelector(".btn"); oBtn.onclick = function(){ var value = oPut.value; if (value.length <= 0 || /^\s+$/.test(value)) { alert("请输入内容"); }else{ alert(value); }; }; </script> </html> 判断输入的全是空格 var account = document.getElementById("account").value.trim(); var password = document.getElementById("password").value.trim(); if (account==""|| password==""){ ? alert('全是空格'); } function test() { var dom = document.getElementById('id'); var value = dom.value; value = value.replace('\s+', ''); //关键是这一句,把value中的空格全replace成空字符串 if(value.length > 0) { } else { alert('全是空格'); } }