.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Enter only numbers in textbox in asp.net

Posted By:satyapriyanayak       Posted Date: February 25, 2014    Points: 40    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  

In this Code Snippets we will know, allow only numbers in textbox in asp.net.
 

Method-1

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Enter_only_numbers_in_textbox._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript" language="javascript">
    function numeric(evt)
    {
   var charCode = (evt.which) ? evt.which : event.keyCode
   if(charCode > 31 && ((charCode >= 48 && charCode <= 57) || charCode == 46))
   return true;
   else
   {
    alert('Please Enter Numeric values.');
    return false;
   }
}
    </script>       
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="TextBox1" runat="server" onkeypress="return numeric(event)"></asp:TextBox>
    </div>
    </form>
</body>
</html>

Method-2

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Enter_only_numbers_in_textbox.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript" language="javascript">
    function numeric( e )
    {
    var unicode = e.charCode ? e.charCode : e.keyCode;
    if( unicode == 8 || unicode == 9 || ( unicode >= 48 && unicode <= 57 ) )
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <input type="text" name="textbox1" id="textbox1" onkeypress="return numeric(event);" />
    </div>
    </form>
</body>
</html>

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend