views:

29

answers:

2

Welcome,

I have textarea and i have small function what allow me to submit message on enter keypress.

$(function(){
    $('input').keydown(function(e){
        if (e.keyCode == 13) {

          $(".sender").click();
            return false;
        }
    });
});

That's work fine, i mean great :)

Next, i want disallow sending empty form. So i was thinking it will be a easy task.

var tag = $("#mytextarea").val();

if(tag.length<1 )
{
//empty
}
else
{
//not empty
}

But it's not working. I also try val=='' I think it count that enter what user hit to send form ;) I was trying alert value, but i got only that enter...

So, how can i check if is empty or not, but counting enter, like empty. If only enter will be found, threat like empty space val==''

Regards

+2  A: 

Try

/\S/.test(tag);

\S is regex for "any non-whitespace character".

So if that pattern matches tag, that means tag contains any non-whitespace character.

if(/\S/.test(tag)) {
   // has content!
}
David Hedlund
Thank You, it's working great.Nice to learn new thing ;)
marc
A: 

You should use regexp for this, also includes cases when you have a carriage return:

if(tag.search(/[^\n\s]/)!=-1)
{
//not empty
}
else
{
//empty
}
Frost.baka