1

I tried using following method to ban users from leaving only space in the input area with no luck:

method one:

var formSub  = $('#formsub').val();

if (formSub == null || formSub == "") {
  return false;
}

method two:

if (formSub.trim() == "" || formSub.trim() == " ") {
  return false;
}

method three:

if ($.trim(formSub) == "" || $.trim(formSub) == " ") {
  return false;
}

Any thought? :)

Aero Wang
  • 8,382
  • 14
  • 63
  • 99

4 Answers4

2

Use a simple regexp:

/\S/.test(formSub)

where \S refers to any non-white space character.

This removes the dependency on trim (not found in IE<=8) and/or jQuery.

  • you are obviously a master of regex! Do you happen to know how to allow only numbers 0-9 and signs inlcuding .+- (yes tel validation). I've found some example but they all limit the length, I don't want to limit the length since most users are international. – Aero Wang Mar 14 '15 at 03:59
  • Someone asked a question here actually, and no valid answer: http://stackoverflow.com/questions/13677058/regex-optionally-allowed-parentheses-dots-dashes-spaces-and-plus-sign-in-pho – Aero Wang Mar 14 '15 at 04:03
  • Okay, I think I got it. It's `[0-9\-\(\)\+\.\s]`, correct me if I am wrong. – Aero Wang Mar 14 '15 at 04:05
  • 1
    @AeroWindwalker That will make sure at least one of the characters is present, but will pass through others. You want `/[^-+0-9()/`, which will return true if any invalid characters are present. This is called a "negated character class" (see the leading `^` character). –  Mar 14 '15 at 04:12
  • `/[^-+.0-9()]/` you mean? It will console log invalid regex with your expression. Also, I've noticed I also need space in the expression. I tried `/[^-+.0-9()\S]/` and it doesn't work, why? – Aero Wang Mar 14 '15 at 04:20
  • Ops, it should be `/[^-+.0-9()\s]/` my bad (with a small s). – Aero Wang Mar 14 '15 at 04:30
1

It should be formSub == null || formSub.trim() === "".

=== and == isn't exactly the same. == "" can means true, and any string is "true".

James Wayne
  • 1,894
  • 14
  • 14
  • 1
    An input element will never have a value of null, and hence you do not need to check for that. –  Mar 14 '15 at 03:21
0

Try:

if (formValue.length === 0 || !formValue.trim()) {
    return false;
}
k0pernikus
  • 60,309
  • 67
  • 216
  • 347
  • It would seem that the left and right side of the condition mean the same thing. If `formValue.length` is 0, then `formValue.trim()` will be falsy anyway. –  Mar 14 '15 at 03:16
  • @torazaburo If `formValue.length` is 0, `formValue.trim()` will not be invoked due to [short-circuit evaluation](http://stackoverflow.com/questions/12554578/does-javascript-have-short-circuit-evaluation) – k0pernikus Mar 14 '15 at 03:23
0

Haha! This is a good one you actually walk around the solution all along.

So here for example, you actually already trimmed all the spaces by using .trim()

if (formSub.trim() == "" || formSub.trim() == " ") {
  return false;
}

Correct would be just,

if (formSub == " ") {
  return false;
}

This will be the most useful, it passes if someone actually wrote something different from spaces, or other invisible characters ;) Google "Javascript Regex for more info"

if (/\S/.test(formSub)) {
   // String is not empty
}
else{
  // String is empty and not usefull
}

Cheers ;) ! +1 Appreciated

Piotr Dajlido
  • 1,982
  • 15
  • 28