2014-10-20 72 views

回答

1

您可以使用正則表達式:

if (str.replace(/[\s\n\r]+/g, "") != "") 
{ 
    // if you remove spaces and line breaks, it doesn't equal nothing 
} 

編輯 - 它應該是更快

if (/\S/.test(str)) 
{ 
    // found something other than spaces or line breaks 

    // "\S" should match any character that is not a space or line break 
} 
+0

感謝豬頭,是缺少的東西雷 – Jim 2014-10-20 16:55:39

0

空間只是一堆空格。所以,你可以用下面的方法來檢查:

if(s.indexOf(' ') >= 0) return true; 

您可能還需要修整的,而不是檢查空格的潛力存在你輸入:

s.trim(); 
0

您可以使用修剪和檢查的長度。

// it is a bunch of spaces 
if(inputValue.trim().length == 0){ /* ... */} 
相關問題