How can I determine if a string only contains spaces, using javascript?

前端 未结 5 1264
滥情空心
滥情空心 2021-02-05 23:24

How can I determine if an input string only contains spaces, using javascript?

5条回答
  •  无人共我
    2021-02-05 23:54

    Another good post for : Faster JavaScript Trim

    You just need to apply trim function and check the length of the string. If the length after trimming is 0 - then the string contains only spaces.

    var str = "data abc";
    if((jQuery.trim( str )).length==0)
      alert("only spaces");
    else 
      alert("contains other characters");
    

提交回复
热议问题