Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to create a textbox of class
System.Web.UI.WebControls.TextBox
and want that the user will be able to enter only digits(numbers).

I have searched a lot on this and the only result that I get is either using KeyPressEventArgs and OnKeyPress method which I think is for Windows Form textbox type or it is implemented using Javascript for onkeypress event of the textbox.

I want to know is there any method by which we can make such a textbox using C# only?

I don't want to validate the textbox. I want the user should be able to type only numbers in the textbox.

Thanks in advance

Regards
Abhisek Majumder
Posted
Updated 14-Oct-17 1:43am
v2

How about using JavaScript to filter the input.

<input name="myInput" id="myInput" onkeypress="return allowOnlyNumber(event);">


VB
function allowOnlyNumber(evt)
{
  var charCode = (evt.which) ? evt.which : event.keyCode
  if (charCode > 31 && (charCode < 48 || charCode > 57))
    return false;
  return true;
}


Note: In the allowOnlyNumber function, it serves my need, you need to modify it as it suit you.
 
Share this answer
 
Comments
Espen Harlinn 20-Feb-11 9:11am    
My 5, that should work :)
Member 10010993 17-Dec-19 1:47am    
Thank You Bro
Will an Ajax solution be of any help to you? If yes, then this[^] might help.
 
Share this answer
 
Comments
Abhisek_Majumder 19-Feb-11 13:03pm    
Thanks for the reply but I'm trying to avoid javascript/ajax. Currently i can make it work via using javascript

<asp:TextBox ID="textbox1" runat="server" Width="150px" MaxLength="8"
onkeypress="if(event.keyCode<48 || event.keyCode>57)event.returnValue=false;" Enabled="False" >
</asp:TextBox>

But i want to solve it using C# only.
Dave Kreskowiak 19-Feb-11 15:38pm    
You cannot do this in C# only. Since ASP.NET code only runs on the server, the only way to do this entirely in C# would be to have a round-trip to theserver on every keystroke. Painfully slow...

Even if you made your own customer TextBox, it would have to use JavaScript on the client end in order to work.
Member 14587636 18-Sep-19 18:48pm    
dd
Abhisek_Majumder 19-Feb-11 15:46pm    
Okay got it thanks :)
Abhisek_Majumder 19-Feb-11 15:47pm    
Still can you tell me the way to do it in C# even if it require server round trips
You can use RegularExpression Validation on the specific text box...The validation expression will be the following
[0-9]*
 
Share this answer
 
Comments
Espen Harlinn 20-Feb-11 9:11am    
Nice and simple, my 5
Yes, we can make our own control.
Refer :
1. http://msdn.microsoft.com/en-us/library/ms229644.aspx#Y100[^]
2. Numeric TextBox[^]
3. Simple Numeric TextBox[^]

Hope this helps.
All the best.
 
Share this answer
 
Comments
Abhisek_Majumder 19-Feb-11 13:06pm    
Thanks for the quick response.

But all the three links are modification of System.Windows.Forms.TextBox but i want to work on System.Web.UI.WebControls.TextBox.

I'm not sure but i dont think those methods can be applied to Web Textbox as they are meant for Form Textbox.
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="TextBoxPrice" Type="Integer" ErrorMessage="CompareValidator" ForeColor="Red" Operator="DataTypeCheck">Numbers Only are allowed..</asp:CompareValidator>
 
Share this answer
 
for example for numbers in the range 0 to 65535

ASP.NET
<asp:textbox id="tbnumber" runat="server" xmlns:asp="#unknown"></asp:textbox>
<asp:regularexpressionvalidator id="number_RegularExpressionValidator" controltovalidate="tbnumber" runat="server" xmlns:asp="#unknown">
ErrorMessage="insert a number (0 to 65535)" 
ValidationExpression="^(0|(\+)?([1-9]{1}[0-9]{0,3})|([1-5]{1}[0-9]{1,4}|[6]{1}([0-4]{1}[0-9]{3}|[5]{1}([0-4]{1}[0-9]{2}|[5]{1}([0-2]{1}[0-9]{1}|[3]{1}[0-5]{1})))))$" 
ValidationGroup="Validator"></asp:regularexpressionvalidator>


regex autor João Batista Neto
 
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