13

I know I can use the following to check if a string is empty in JavaScript:

 if(Message != '')

How would I check to see if a string 'Message' in this case - is empty and doesn't contain a number of spaces. eg:

 '    '

would I need to use regular expressions?

1
  • This is Javascript not jQuery. Commented Nov 13, 2012 at 1:08

1 Answer 1

39

jQuery doesn't replace Javascript. You can use:

if (Message.replace(/\s/g, "").length > 0) {
    // Your Code
}

Having said that, if you really want jQuery version, try this:

if ($.trim(Message).length > 0) {
    // Your Code
}

Or, so long as you're only targeting IE9+ and modern browsers, you can use the built in trim function.

if (Message.trim().length > 0) {
    // Your Code
}
Sign up to request clarification or add additional context in comments.

3 Comments

@EugeneXa Yes, its not really needed, But I usually add it for readability (atleast for me)
works great... I really need to skill up more on javascript... jquery is the easy route... cheers... thx :)
There's an extra closing bracket I guess. Message.replace(/\s/g, "").length > 0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.