js判斷輸入是否有空格
阿新 • • 發佈:2019-02-08
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<script>
function checkSpace() {
var obj = document.getElementById("name");
var str = obj.value;
if (str.indexOf(" ") >=0) alert("輸入有空格!");
obj.value = str.replace(/\s/g, ""); // 這句話可以強制刪除所有空格
}
</script>
</head>
<body>
<input id="name" type="text" value="">
<input type="button" value="提交" onclick="checkSpace()">
</body>
</html>