How can I determine if an input string only contains spaces, using javascript?
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.
You can do this by simply using trim. var str = " "; if (str. trim(). length == 0) { console.
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.
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");
if (!input.match(/^\s*$/)) {
//your turn...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With