In my Chat application project, I am trying to restrict spaces when the user has just pressed Space and then Enter. pressing Enter will show entered Text into a div. Please see the comments in the code to understand clearly.
#txtmsg --> ID of TextBox field
$('#txtmsg').keypress(function (e)
{
if (e.which == 13)
{
//Disable default function of Enter key
e.preventDefault();
//Checks whether the user has entered any value or not
if ($('#txtmsg').val().length > 0)
{
//if(user has not entered only spaces....)?
//transfers the entered value in the text field to div
chat.server.send($('#txtmsg').val());
$('#txtmsg').val('');
}
}
});
See http://api.jquery.com/jQuery.trim/ :