javascript 空白
javascript 判斷輸入值是否空白 <html> <head> <script> // 去左右空白 function trim(strvalue) { ptntrim = /(^\s*)|(\s*$)/g; return strvalue.replace(ptntrim,""); } // 去左空白 function rtrim(strvalue) { ptnltrim = /^\s*/g; return strvalue.replace(ptnltrim,""); } // 去右空白 function ltrim(strvalue) { ptnrtrim = /\s$/g; return strvalue.replace(ptnrtrim,""); }
</script>
<script> function Test() { if (trim(document.getElementById("data").value) == "") { document.getElementById("data").value = ""; alert("Can't be Empty"); } else { alert("Hello " + document.getElementById("data").value); } } </script> </head> <body> <input type="text" id="data" name="data"> <input type="button" id="tt" name="tt" value="Click" onclick="Test();"> </body> </html>