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

148
Views
Restrict entering Spaces in TextBox in Javascript

In my code have to allow numerical alphabets and special characters. but in my code not accepting the space and special characters except @. Here is my code.

allowAlphaNumericSpace(e: any) {
var code = 'charCode' in e ? e.charCode : e.keyCode;
if (
  !(code > 47 && code < 58) && // numeric (0-9)
  !(code >= 64 && code <= 91) && // upper alpha (A-Z)
  !(code > 96 && code < 123)
) {
  // lower alpha (a-z)
  e.preventDefault();
}
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

charCode is non-standard, deprecated, and not foreseen for keydown events, it may be available, but always 0.

keyCode is also deprecated.

Use e.key:

const accepted = " ,;!?";
function allowAlphaNumericSpace(e) {
  var code = e.key;
  if (isNaN(code) && code.toUpperCase() == code.toLowerCase() 
                  && !accepted.includes(code))  {
    e.preventDefault();
  }
}

document.querySelector("input").addEventListener("keydown", allowAlphaNumericSpace);
<input>

If some other characters need to be accepted, it is easily adapted.

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!