禁用键盘键 [英] Disabling keyboard keys

查看:105
本文介绍了禁用键盘键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的文本框中,我想只输入货币类型编号,即如果输入点并再次按下点键,则不会采用任何字母输入,也不会采用任何字母输入...

有人可以帮助我....

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

推荐答案

使用正则表达式和屏蔽来改变原生解决方案。



http://stackoverflow.com/questions/7301447/restrict-characters -allowed-in-textboxentering-money-amount [ ^ ]

http://stackoverflow.com/questions/12209484/masking-textbox-to-accept-only-decimals [ ^ ]
Alter native solutions using regex and masking.

http://stackoverflow.com/questions/7301447/restrict-characters-allowed-in-textboxentering-money-amount[^]
http://stackoverflow.com/questions/12209484/masking-textbox-to-accept-only-decimals[^]


这是您必须放入TextBox_KeyPress事件的代码

只需要数字



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

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





此代码是用VB.net编写的,你可以在其中更改C#轻松



This code is written in VB.net you can change in it C# easily


这将有助于你... :)



使用键盘事件进行表单验证:仅限数字 [ ^ ]



或者看看这段代码:



This will help you... :)

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

Or see this code :

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





参考:文本框应该只接受数字和点 [ ^ ]


这篇关于禁用键盘键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆