Click here to Skip to main content
15,898,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
in c#, i wants user only enter digit data to a textbox (character 0 to 9)
how can i do it?
thanks very much
Posted
Comments
bbirajdar 3-Jul-13 5:57am    
use validators

 
Share this answer
 
Use this Javascript

XML
<script type="text/javascript" language="javascript">
 function NumericOnly(obj)
        {
                if(obj.value.length>0)
                {
                obj.value = obj.value.replace(/[^\d]+/g, ''); // This would validate the inputs for allowing only numeric chars
                }
        }
    </script>


& call this function in text box as

<asp:TextBox ID="TextBox1" runat="server" ToolTip="Enter Only Numeric" onkeyup="NumericOnly(this);"></asp:TextBox>
 
Share this answer
 
 
Share this answer
 
Simplest solution:

C#
<asp:regularexpressionvalidator id="RegularExpressionValidator3" runat="server" errormessage="Enter digits Only" controltovalidate="your textbox id" validationexpression="^[0-9]\d*(\.\d+)?$" xmlns:asp="#unknown">Enter digits only</asp:regularexpressionvalidator>


Regards..:laugh:
 
Share this answer
 
v2
use ajax filter textbox extender and set valid character 0-9
 
Share this answer
 
function NumericTextBox(input,event)
{

var keyCode = event.which ? event.which : event.keyCode;


if(parseInt(keyCode)>=48 && parseInt(keyCode)<=57)
{
return true;
}
if(parseInt(keyCode)==8 || parseInt(keyCode)==46 || parseInt(keyCode)==9){return true;}
return false;
}
 
Share this answer
 
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
}
 
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