0

This is going to be dumb, but I can't figure this out, nor find the answer

scope.newNote = "";
scope.addNote = function(note) {
    console.log(note);
    if (!note || note === '' || note.trim().length === 0) {
        return;
    }
}

HTML

<wysiwyg textarea-class="form-control" textarea-required ng-model="newNote" enable-bootstrap-title="true"></wysiwyg>

<button class="btn btn-default right" ng-click="addNote(newNote); newNote = null;">Add</button>

When I enter a bunch of spaces, this prints "'&''n''b''s''p'';''&''n''b''s''p'';'"

Hence, the note.trim().length is equal to 12

How do I check for strings that are just spaces?

Thanks

4
  • 1
    Paste the relevant html here, btw your tag should have ng-click="addNote()" and not on-click Commented Jan 1, 2015 at 13:05
  • share your complete controller. shouldn't it be '$scope' instead of 'scope'? Commented Jan 1, 2015 at 13:10
  • How do you enter text that it contains &nbsp;? Commented Jan 1, 2015 at 13:12
  • Updated. I'm using a textarea to enter text Commented Jan 1, 2015 at 13:15

1 Answer 1

1

Your question is:

How do I check for strings that are just spaces?

The answer is that you need is a simple regular expression like this:

if (/^\s*$/.test(note)) {
  // stuff
}

Explanation

^ -> beginning of string. With this symbol we say: we want to start the matching from the start.

\s -> match whitespace characters

* -> match 0 or more of the previous characters (In this example it is the \s) . Thanks to the fact that we can match 0 characters the empty string case ( "" ) is covered.

$ -> end of string. Match the end of string with the expression. Together with the ^ we say: match the whole string, not just a part of it.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.