Restrict user to enter numeric value in textbox using javascript

Restrict user to enter numeric value in textbox using javascript

01 Feb 2024
1 min read
Learn via Video Course & by Doing Hands-on Labs

JavaScript For Beginners Free Course

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.

 <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;
 event.charCode= 0

<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 free javascript skill challenge to evaluate your skill

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


Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this