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

382
Views
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 answers
Answer question

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 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!