-1

Hi there everyone how can I detect if the user input has just empty spaces?

Detect this:

input = ' ';
input = '          ';
etc...

DO NOT detect this:

input 'hi there';

I just want to detect if there is a space in the input and that's it?


Here is what i have tried:

function whitespaceDetect(s) {
  return /\s/g.test(s);
}

if(whitespaceDetect(document.getElementById("name").value)) {
    alert('error!');
}

but this comes up with an error if the name is "john doe";

What way is the fastest to process? How can i achieve this with or without jquery?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
pixie123
  • 929
  • 3
  • 14
  • 27

1 Answers1

1

function trim

str.trim() === ''
Ele
  • 33,468
  • 7
  • 37
  • 75
  • I agree with this. It makes more sense than trying to check for "empty spaces". +1 – BSK Apr 22 '18 at 00:09