Click here to Skip to main content
15,886,860 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can we insert only integer no. in textbox.
Posted
Comments
Arul R Ece 19-Mar-12 5:41am    
how we export the grid data in Excel

You can use Copmpare validator for this
C#
<asp:textbox id="txtCOB" maxlength="3" runat="server" xmlns:asp="#unknown">
size="2"></asp:textbox>
<asp:comparevalidator runat="server" xmlns:asp="#unknown">
ControlToValidate="txtCOB"
EnableClientScript="True"
id="valCoB"
ErrorMessage="Please enter a number"
Operator="DataTypeCheck"
Type=Integer></asp:comparevalidator>
 
Share this answer
 
hey,
using javascript u can do this otherwise use ajax
javscript code--

<pre lang="cs">function onlyNumber(evt) {
if (!(evt.keyCode &gt;= 48 &amp;&amp; evt.keyCode &lt;= 57)
return false;</pre>
and in page load event write following line
txtNumber.Attributes["onkeypress"] = "return onlyNumber(event);";

Best luck
 
Share this answer
 
There are a number of ways.
My personally preference would be not to use a TextBox, but to use a NumericUpDown control instead, or possibly a MaskedTextBox.
However, these are only available in WinForms projects, and if you are indeed using ASP then Javascript is your best option. (otherwise you can validate it at the server, but that's a PITA for the user).
There is a javascript solution here: http://www.cambiaresearch.com/articles/39/how-can-i-use-javascript-to-allow-only-numbers-to-be-entered-in-a-textbox[^]
 
Share this answer
 
hello there,

you can use this javascript on textbox any event like keyup or keydown :-
C#
function Numeric(Id)
{
If(IsNaN(Id.Value))
{
alert('only Numeric');
}
}
 
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