18

Help me pls

if(value == ""){
// do anything
}

but I need to check space " " (2,3,... space is include) is the same way of empty String

ps. sorry in my English

  • @Andrew - It's not a duplicate at all. That other question has nothing to do with detecting strings containing only spaces. – RJM Apr 08 '16 at 03:40
  • @RJM There are numerous answers on that question that handle: null/undefined and only-whitespace input. See, for example, [this answer](http://stackoverflow.com/a/3261380/279608) – Andrew Apr 08 '16 at 03:45
  • sorry man I need to check String with whitespace only is same as empty String – JinkleBell Surcharoen Apr 08 '16 at 03:45

2 Answers2

12

A regex can easily solve this problem.

if (/^ *$/.test(value)) {
    //string contains 0+ spaces only
}

If you need to include null also, then add !value ||.

If you need to include newlines, tabs, and the like, then use /^\s*$/ for the regex.

4castle
  • 32,613
  • 11
  • 69
  • 106
-1

I'm not sure I'm understanding correctly. Do you mean like:

if (value == "" || value == " "){
// do anything
}
RJM
  • 1,164
  • 9
  • 21