Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up
Restrict user to enter numeric value in textbox using javascript

Restrict user to enter numeric value in textbox using javascript

Intermediate 1 min read 15 Jul 2025 10.9K views
Restrict user to enter numeric value in textbox using javascript

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

Test yourself in 5 minutes

Take the free Javascript skill challenge to see your strengths and the gaps to fix next.

Start the challenge
Share LinkedIn X Facebook WhatsApp

About the author

Shailendra Chauhan

Shailendra Chauhan

Microsoft MVP, Founder & CEO at ScholarHat

He is a renowned Speaker, Solution Architect, Mentor, and 11-time Microsoft MVP (2016–2026). 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.

Keep learning

Courses that go with this tutorial