Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
In my textbox i want to enter only currency type numbers, i.e. if dot is entered and dot key is pressed again, it won't take and none of the alphabetical entry it will take...
can someone help me with this....
Posted
Comments
johannesnestler 16-Jul-14 5:23am    
also think about globalization - we german-speakers use the comma as decimal seperator....
Sergey Alexandrovich Kryukov 18-Jul-14 2:37am    
It all depends on which TextBox. Full type name, please.
—SA

Here is a code you have to put in the TextBox_KeyPress event
It will take only numbers

VB
Private Sub TextBoxes_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    If Asc(e.KeyChar) <> 8 Then
      'Allow only numbers to enter
      If Not (Char.IsDigit(CChar(CStr(e.KeyChar))) Or e.KeyChar = ".") Then
        e.Handled = True
      End If
      'Allow only one decimal point
      If e.KeyChar = "."c AndAlso TryCast(sender, TextBox).Text.IndexOf("."c) > -1 Then
         e.Handled = True
      End If
    End If
  End Sub


This code is written in VB.net you can change in it C# easily
 
Share this answer
 
This will help you... :)

Form validation using the keyboard events : Numbers only[^]

Or see this code :

HTML
<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"  önkeypress="return isNumberKey(event)">
           type="text" name="txtChar">
  </input></body>
</html>


Reference : Text box should only accept only numbers and dot[^]
 
Share this answer
 
v2
C#
private void TextBoxes_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
	if (Strings.Asc(e.KeyChar) != 8)
         {
           //Allow numbers only
	   if (!(char.IsDigit(Convert.ToChar(Convert.ToString(e.KeyChar))) | e.KeyChar == ".")) 
            {
	      e.Handled = true;
	    }
	   //Allow decimal only
	 if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1) 
            {
	      e.Handled = true;
	    }
	  }
}
 
Share this answer
 
Comments
ShobuGpta 16-Jul-14 1:22am    
this code is fine but it's also disabling backspace and, code for decimal is not working, when i press dot key its shows exception even for the first time(Object reference not set to an instance of an object).
and i dont understand this code "if (Strings.Asc(e.KeyChar) != 8)" why have u used this
ShobuGpta 16-Jul-14 1:53am    
instead of "(sender as TextBox).Text.IndexOf('.') > -1" i used "textbox.text.contains(".")", it's working without any problem
johannesnestler 16-Jul-14 5:25am    
try to run your "naive" code in another culture... Don't hardcode the "." use System.Globalization.CultureInfo.CurrentUICulture.NumberFormat.CurrencyDecimalSeparator instead
If you want to use jQuery then this code can help you...

jquery
$(document).ready(function() {
    $("#txtboxToFilter").keydown(function (e) {
        // Allow: backspace, delete, tab, escape, enter and .
        if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
             // Allow: Ctrl+A
            (e.keyCode == 65 && e.ctrlKey === true) ||
             // Allow: home, end, left, right
            (e.keyCode >= 35 && e.keyCode <= 39)) {
                 // let it happen, don't do anything
                 return;
        }
        // Ensure that it is a number and stop the keypress
        if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
            e.preventDefault();
        }
    });
});
 
Share this answer
 
Comments
johannesnestler 16-Jul-14 5:26am    
try to run your "naive" code in another culture... Don't hardcode the "."

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