Please enable Javascript to correctly display the contents on Dot Net Tricks!
 
Ready to master latest .NET, JavaScript framework and Mobile development skills?
Join our training programs and take your career to the next level!

Restrict user to enter numeric value in textbox using javascript

Posted By : Shailendra Chauhan, 11 Jun 2012
Updated On : 29 Jan 2013
Total Views : 4,925   
 
Keywords : disable characters in asp.net textbox,restrict characters in textbox,disable characters in textbox pdf

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;
 }
else
{
 event.charCode= 0
}
 }
}
</script>
<asp:TextBox ID="TextBox1" runat="server" onkeypress="return checkNumeric(event); ></asp:TextBox> 
 
Recommended Articles!
 
About the Author
Hey! I'm Shailendra Chauhan author, developer with more than 5 years of hand over Microsoft .NET technologies. I am a .NET Consultant, founder & chief editor of www.dotnet-tricks.com and www.webgeekschool.com. I am author of books ASP.NET MVC Interview Questions and Answers & LINQ Interview Questions and Answers.
I love to work with web applications and mobile apps using Microsoft technology including ASP.NET, MVC, C#, SQL Server, WCF, Web API, Entity Framework,Cloud Computing, Windows Azure, jQuery, jQuery Mobile, Knockout.js, Angular.js and many more web technologies. More...
 
Free .NET Interview Books
 
Browse By Category
Learn In Hindi
 
 
Like us on Facebook