13

I'm submitting some comments via $.ajax. However I want to check to make sure that some content is entered in the input box. At the moment I am using this:

if ($("#comments").val().length != 0){

However this only works if a user leaves in the input blank, if the leave a space or return then it will pass. How can I improve this if?

4 Answers 4

22

Remove whitespace before checking for empty:

if ($("#comments").val().replace(/^\s+|\s+$/g, "").length != 0){
2
  • 2
    would this be any more useful than just using a trim?
    – Chris
    Aug 16, 2010 at 12:28
  • 1
    @Chris - no - I just didn't know about jQuery's trim function. Aug 16, 2010 at 12:39
17

you might go for a trim-function ... google give us some nice links. btw ... jQuery does have a trim-function

eg.

var trimmedValue = jQuery.trim($('#comments').val());
if (trimmedValue.length > 0) {
   // TODO
}
4
  • Untested but I would think you could go further and do jQuery.trim($("#comments").val()).length since the trim returns a string anyway. Not that the extra variable is really going to hurt anything.
    – thismat
    Feb 4, 2010 at 6:31
  • as i'm always a friend of introducing extra variables, to have some possibility to debug, i would leave it that way :)
    – user57508
    Feb 4, 2010 at 6:33
  • Can't argue that. I'm just in the habit (good or bad) of avoiding extra variables if I won't be reusing them. I could see a good case of reusing this if you're going to validate the actual content though.
    – thismat
    Feb 4, 2010 at 6:38
  • yeah :) as i'm always validating & validating & ... i would reuse this variable!
    – user57508
    Feb 4, 2010 at 6:42
2

In JavaScript

//Following all return true
"" == false
" " == false
"  " == false
"   " == false
//Not related to question but interesting
//Following all (also) return true
!" " == false
!"  " == false
!"   " == false
//Except
!"" == false //returns false

(Verified with Firebug console and Chromium's Developer Tools console).

1
  • Ok, aldo it's a very weird way to check a statement, I give you that it works: jsfiddle.net/cn2YH/2
    – jerone
    Aug 16, 2010 at 14:51
1

I know this is an incredibly late answer, but I am using this...

var self = $(this).val(); 
if (!self || self[0] == ' '){
  //////do something
}

and it works well! hope someone else can get some use out of it!

4
  • This is not a good idea. What about the case when the user inputs " valid string"? You probably wouldn't want to discard that.
    – F. Orvalho
    Nov 29, 2012 at 10:06
  • I guess it's possible, but why would anyone want an empty value for their first char? Dec 4, 2012 at 23:15
  • Or should I say need an empty value instead of want. Dec 4, 2012 at 23:17
  • It's a user input... never trust users to do the right thing!
    – F. Orvalho
    Dec 10, 2012 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.