I installed <textarea>and functions that pick up keystrokes. I set where, if the user presses Enter, the text entered in the text area will be sent to the database.
However, I want to prevent sending empty text by simply pressing Enter and sending. I also noticed that when I enter a new line, a new line is created, so I canβt just check if the text is "" or its length is 0, because the second time there will be a new line.
jQuery for keyboard detection:
$(document).ready(function(){
$('.active-buddy-tab div#chat-window form#chat-message textarea#message').live('keydown', function(event) {
var key = event.which;
if (key >= 33) {
var maxLength = $(this).attr("maxlength");
var length = this.value.length;
if (length >= maxLength) {
event.preventDefault();
}
}
});
$('.active-buddy-tab div#chat-window form#chat-message textarea#message').live('keyup', function(e) {
if (e.keyCode == 13) {
var text = $(this).val();
var maxLength = $(this).attr("maxlength");
var length = text.length;
var to = $('.active-buddy-tab div h3 p#to').text();
if (length <= maxLength + 1) {
chat.send(text, from, to);
chat.update(from, to);
$(this).val("");
} else {
$(this).val(text.substring(0, maxLength));
}
}
});
});
So how can I prevent sending an empty message? I apologize if this is really simple, maybe I think too much about it.
Thank you Christo