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

96
Views
How to restrict user from entering number in to an input field with type as text?

How to disable the user from entering a number in to input field . When we use type as number we are restricted from entering text in to the input .The same way how to achieve the vice versa . Someone help i need this to complete my assignment.

<input type="text" name="name" />
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can add an input event listener to the input which gets the input's value, replaces every numeric character (with String.replace), then assigns the result back to the input's value:

const input = document.querySelector('input');
input.addEventListener('input', function(){
  this.value = this.value.replace(/[^\D]/g,'')
})
<input type="text" name="name" />

about 4 years ago · Juan Pablo Isaza Report

0

You can listen to keydown, and if the key pressed is a number, just call event.preventDefault() on the keyboard event.

const input = document.querySelector('input');
input.addEventListener('keydown', function(event){
  if((/\d/g).test(event.key)) event.preventDefault();
})
<input type="text" />

about 4 years ago · Juan Pablo Isaza Report

0

You can use 1 line function to prevent any number in the input with help of regular expression

But it is not recommended to use inline function , here is provided only to tell about a different approach

See here to know more about regular expression

<input type="text" name="name" oninput=" this.value = this.value.replace(/[0-9]/g,'')" />

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!