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

261
Views
Disabling space as first character in input field doesnt work on Chrome Android/App

I have this code that basically disables adding in an empty space inside an input field, however it doesn't seem to run on Chrome Mobile/App/Android.

Does anyone have any solutions?

<script>
document.getElementById('firstname').addEventListener('keydown', function(evt){
  if (this.value.length === 0 && evt.which === 32) evt.preventDefault();
});
</script>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

According to https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent, KeyboardEvent.which is deprecated. Use KeyboardEvent.key instead.

Also see list of key codes.

document.getElementById('firstname').addEventListener('keydown', function(evt){
  if (this.value.length === 0 && evt.key === " ") evt.preventDefault();
});
<input id="firstname">

about 4 years ago · Juan Pablo Isaza Report

0

You can use KeyboardEvent.code instead

document.getElementById('firstname').addEventListener('keydown', function(evt){
  if (this.value.length === 0 && evt.code === 'Space') 
    evt.preventDefault();
});
<input id="firstname">

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!