Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 595
123 Avatar asked May 10 '12 05:05

123


People also ask

How do I check if a string contains spaces?

In order to check if a String has only unicode digits or space in Java, we use the isDigit() method and the charAt() method with decision making statements. The isDigit(int codePoint) method determines whether the specific character (Unicode codePoint) is a digit. It returns a boolean value, either true or false.

How do you check if string is empty or has only spaces in it using JavaScript?

You can do this by simply using trim. var str = " "; if (str. trim(). length == 0) { console.

How do I check if a string is empty space?

1. String isBlank() Method. This method returns true if the given string is empty or contains only white space code points, otherwise false . It uses Character.


2 Answers

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");
like image 121
Pranay Rana Avatar answered Oct 18 '22 08:10

Pranay Rana


if (!input.match(/^\s*$/)) {
    //your turn...
} 
like image 9
Chuck Norris Avatar answered Oct 18 '22 07:10

Chuck Norris