Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello Friends all of you,

My project is based on Asp.net c#

User must enter only numbers, not alphabets in textbox.

Please can u suggest me,how to do this.

Please help help help

Thanks in advance.
Posted
Comments
Prosan 14-Jul-12 9:30am    
look my solution 5

There are ways to make this happen, using regular expression, JavaScript validation, etc.

I would prefer Masked Textbox for it: Enhanced Textbox Control[^]
 
Share this answer
 
Comments
Sebastian T Xavier 14-Jul-12 5:46am    
point blank +5
M@anish 14-Jul-12 13:50pm    
how to print command on button click in asp.net
Google is your friend: Be nice and visit him often. He can answer questions a lot more quickly than posting them here...

A very quick search gave over 400,000 hits: Google "only numbers in textbox asp.net"[^]

In future, please try to do at least basic research yourself, and not waste your time or ours.
 
Share this answer
 
XML
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1"
        ErrorMessage="Enter a valid number" ValidationExpression="^\d$"></asp:RegularExpressionValidator>
 
Share this answer
 
hi
try like this javascript. it helps you...

JavaScript
<html>
  <head>
    <script language="Javascript">
       <!--
       function isNumberKey(evt)
       {
          var charCode = (evt.which) ? evt.which : event.keyCode
          if (charCode != 46 && charCode > 31 
            && (charCode < 48 || charCode > 57))
             return false;

          return true;
       }
       //-->
    </script>
  </head>
  <body>
    <input id="txtChar" onkeypress="return isNumberKey(event)">
           type="text" name="txtChar">
  </input></body>
</html>


regards
sandeep
 
Share this answer
 
look my written article and download it in this code i have give the js for it
How to Add and Use Javascript Files's Functions in DLL[^]
 
Share this answer
 
v2
Comments
Prosan 14-Jul-12 9:27am    
sorry i have given wrong link see this link
Prosan 14-Jul-12 9:30am    
if your problem solved accept it as answer. and upvote this article
C#
private void txtmobileno_TextChanged(object sender, EventArgs e)
    {
        if (!System.Text.RegularExpressions.Regex.IsMatch("[^0-9]", txtmobileno.Text))
        {
            lblErrormsg.Text = "Please enter only numbers.";
        }
        else
        {
            lblErrormsg.Text = string.Empty;
        }
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900