Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

369
Views
¿Cómo verificar si las entradas son palabras, espacio y nueva línea?

 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>

Después de eso probé esto

 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; } } });

Quiero validar si el usuario está ingresando solo como palabra. Cuando el usuario primero da entrada de o \n solamente, entonces el usuario no debe poder enviar mensajes.

¿Por qué ambos códigos no funcionan?

Por favor, ¿alguien puede decirme cuál es la lógica detrás de esto?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Siempre puede usar Expresión regular para este tipo de consulta. Le ayuda a superar fácilmente el problema de entrada de espacio y nueva línea.

Consulte este artículo para usar RegEx en javascript. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!