Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether the input text field contains only white spaces?

Tags:

javascript

What is the easiest way to check in Javascript whether the input text field is empty (contains nothing or white spaces only)?

like image 567
Misha Moroshko Avatar asked Apr 18 '10 12:04

Misha Moroshko


People also ask

How do I check if input is only spaces?

To check if a string contains only spaces, call the trim() method on the string and check if the length of the result is equal to 0 . If the string has a length of 0 after calling the trim method, then the string contains only spaces.

How do I know if a string contains white space?

/^\s+$/ is checking whether the string is ALL whitespace: ^ matches the start of the string. \s+ means at least 1, possibly more, spaces. $ matches the end of the string.

Does isEmpty check for space?

Both methods are used to check for blank or empty strings in java. The difference between both methods is that isEmpty() method returns true if, and only if, string length is 0. isBlank() method only checks for non-whitespace characters. It does not check the string length.

How do I check if a string contains only space in flutter?

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


2 Answers

var str = document.getElementById("myInput").value;
if (str.match(/^\s*$/)) {
    // nothing, or nothing but whitespace
} else {
    // something
}
like image 151
karim79 Avatar answered Oct 17 '22 00:10

karim79


You are looking for something like trim function, right?

like image 23
Sarfraz Avatar answered Oct 17 '22 00:10

Sarfraz