Click here to Skip to main content
13,794,093 members
Rate this:
 
Please Sign up or sign in to vote.
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 19-Feb-11 6:50am
Updated 14-Oct-17 2:43am
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

How about using JavaScript to filter the input.

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


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.
  Permalink  
Comments
Espen Harlinn 20-Feb-11 9:11am
   
My 5, that should work :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Will an Ajax solution be of any help to you? If yes, then this[^] might help.
  Permalink  
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.
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
Espen Harlinn 20-Feb-11 9:10am
   
Good reply, my 5
Abhinav S 20-Feb-11 10:10am
   
Thanks.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

You can use RegularExpression Validation on the specific text box...The validation expression will be the following
[0-9]*
  Permalink  
Comments
Espen Harlinn 20-Feb-11 9:11am
   
Nice and simple, my 5
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="TextBoxPrice" Type="Integer" ErrorMessage="CompareValidator" ForeColor="Red" Operator="DataTypeCheck">Numbers Only are allowed..</asp:CompareValidator>
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

for example for numbers in the range 0 to 65535

<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
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.181207.3 | Last Updated 14 Oct 2017
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100