js判斷字串是否為空字串
阿新 • • 發佈:2019-02-12
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>js判斷輸入是否為空</title> <script type="text/javascript" src="js/jquery.min.js"></script> </head> <body> <div> <input type="text" id="text" /> <input type="button" id="pan" value="點選" /> </div> <script> $(function() { $("#pan").click(function(){ var txt=$("#text").val(); console.log(txt); if(!isEmpty(txt)){ alert(txt); }else{ alert("輸入不能為空") } }); }); function isEmpty(str) { if(str === undefined) { return true; } else if(str == null) { return true; } else { var reg = /^\s*$/; return reg.test(str); } } </script> </body> </html>