3
I’m making an application of chat with Websocket.
I used contenteditable="true"
in a <div/>
, because I needed to put so to facilitate in other functions.
On the part of input
that sends the message to other users I’m having a problem, when the user type only blank spaces and click send, the message is sent, but I want when he type only blank spaces do not send the message.
Could someone please help me to make this code in Javascript or with Jquery itself?
HTML
<div id="message" contenteditable="true">Digite sua mensagem</div>
jQuery
// ENVIA COM O ENTER
$('#message').on('keydown', function(e) {
var mymessage = $('#message').text(); //get message text
var myname = $('.myname').text(); //get user name
var mensagem = 'mensagem='+mymessage;
var to = $(this).attr('id');
var code = e.which || e.keyCode;
if (e.which == 13 && e.shiftKey == false) {
if (code == 13) {
if (mensagem != '') {
//$(".mensagens-unitarias").animate({"scrollTop": $('.mensagens-unitarias')[0].scrollHeight}, "fast");
$.ajax({
type: 'POST',
url : 'sys/teste.php',
data: 'nome='+myname+'&mensagem='+mymessage,
success: function(html){}
});
if (mymessage == "") { return false; }
//prepare json data
var msg = {
message: mymessage,
name: myname,
color : '<?php echo $colours[$user_colour]; ?>'
};
//convert and send data to server
websocket.send(JSON.stringify(msg));
}
}
return false;
}
})
Can you put the code that does this check? or the code that sends this input?
– Sergio
I can. See the edited question.
– David Damasceno
If I’m not mistaken, it is only possible to validate with HTML5, using the attribute Pattern of a
input
. But since you’re doing this in a div, the way is to use JS.– Renan Gomes