Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

374
Visualizações
How to check whether inputs are words, space and newLine?

let msgInpt = document.getElementById("message");
msgInpt.addEventListener("keydown", function(entEvt){
    if (!msgInpt.innerText === ' ') {
        if (entEvt.code === 'Enter' && !entEvt.shiftKey) {
            entEvt.preventDefault();
            postChat();
        }
    } else {
        if (entEvt.code === 'Enter' && !entEvt.shiftKey) {
           entEvt.preventDefault();
           return false;
        } else if(entEvt.code === 'Enter' && entEvt.shiftKey) {
            entEvt.preventDefault();
            return false;
        } else if (entEvt.code === 'Space') {
            entEvt.preventDefault();
            return false;
        }
    }
});
div{
  background-color: #f2f2f4;
  padding: 12px;
  height: 150px;
  border: 2px solid #333;
  border-radius: 10px;
  box-shadow: 0px 0px 12px 4px rgba(0,0,0,0.18);
}
<div contenteditable="true" id="message"></div>

After that I tried this

msgInpt.addEventListener("keydown", function(entEvt){
    if (msgInpt.innerText) {
        if (entEvt.code === 'Enter' && !entEvt.shiftKey) {
            entEvt.preventDefault();
            if (!msgInpt.innerText === ' ') {
                postChat();
            }
        }
    } else {
        if (entEvt.code === 'Enter' && !entEvt.shiftKey) {
           entEvt.preventDefault();
           return false;
        } else if(entEvt.code === 'Enter' && entEvt.shiftKey) {
            entEvt.preventDefault();
            return false;
        } else if (entEvt.code === 'Space') {
            entEvt.preventDefault();
            return false;
        }
    }
});

I want to validate whether the user is giving input as word only. When user First gives input of or \n only, then user must not be able to send messages.

Why Both the codes are not working?

Please can anybody tell me what is the logic behind this

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can always use Regular Expression for this kind of query. It helps you to overcome the space and newline input problem easily.

Refer this article for using RegEx in javascript. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda