如何使用javascript确定字符串是否只包含空格? [英] How can I determine if a string only contains spaces, using javascript?

查看:71
本文介绍了如何使用javascript确定字符串是否只包含空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用javascript确定输入字符串是否只包含空格?

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

推荐答案

另一个好帖子:更快的JavaScript修剪

你只需要申请 trim 功能并检查字符串的长度。如果修剪后的长度为0 - 则字符串仅包含空格。

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");

这篇关于如何使用javascript确定字符串是否只包含空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆