Restrict user to enter numeric value in textbox using javascript

Restrict user to enter numeric value in textbox using javascript

15 Jul 2025
Intermediate
8.83K Views
1 min read
Learn with an interactive course and practical hands-on labs

Free Javascript Course Online

Due to some reasons (like don’t allow to enter chars in Price TextBox), we restrict users to enter chars in TextBox. We can implement this functionality by using below method. To learn more about handling such cases and other JavaScript techniques, you can enroll in a Free Online JavaScript Course and enhance your skills!

 <script type="text/javascript">
 function checkNumeric(event) {
var kCode = event.keyCode || event.charCode; // for cross browser check

//FF and Safari use e.charCode, while IE use e.keyCode that returns the ASCII value 
if ((kCode > 57 || kCode < 48) && (kCode != 46 && kCode != 45)) {
//code for IE
 if (window.ActiveXObject) {
 event.keyCode = 0
 return false;
 }
else
{
 event.charCode= 0
}
 }
}
</script>

<asp:TextBox ID="TextBox1" runat="server" onkeypress="return checkNumeric(event); ></asp:TextBox> 

Read More: 50+ Javascript Interview Questions and Answers

Read More: Javascript Developer Salary in India

Take our Javascript skill challenge to evaluate yourself!

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.

GET FREE CHALLENGE

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at ScholarHat)

He is a renowned Speaker, Solution Architect, Mentor, and 10-time Microsoft MVP (2016–2025). With expertise in AI/ML, GenAI, System Design, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development, he bridges traditional frameworks with next-gen innovations.

He has trained 1 Lakh+ professionals across the globe, authored 45+ bestselling eBooks and 1000+ technical articles, and mentored 20+ free courses. As a corporate trainer for leading MNCs like IBM, Cognizant, and Dell, Shailendra continues to deliver world-class learning experiences through technology & AI.
Live Training - Book Free Demo
Azure AI Engineer Certification Training
05 Oct
10:00AM - 12:00PM IST
Checkmark Icon
Get Job-Ready
Certification
Azure AI & Gen AI Engineer Certification Training Program
05 Oct
10:00AM - 12:00PM IST
Checkmark Icon
Get Job-Ready
Certification
AWS Developer Certification Training
05 Oct
05:30PM - 07:30PM IST
Checkmark Icon
Get Job-Ready
Certification
Advanced Full-Stack Java Developer Certification Training Course
12 Oct
08:30PM - 10:30PM IST
Checkmark Icon
Get Job-Ready
Certification
ASP.NET Core Certification Training
12 Oct
08:00PM - 10:00PM IST
Checkmark Icon
Get Job-Ready
Certification
Accept cookies & close this