禁用文本框中单个字符的输入

时间:2016-09-01 19:15:18

标签: vb.net

我有一个应用程序可以与第三方票务软件一起提交/更新票证。我遇到的问题是,售票软件读取字符:作为新输入。我要做的是仅禁用:从输入文本框。

我做了一些研究,并找到了方法,禁用字母/数字/特殊,但不是只有一个。

非常感谢任何帮助。

4 个答案:

答案 0 :(得分:3)

试试这个:

 Private Sub txtchange() Handles TextBox1.TextChanged
    TextBox1.Text = TextBox1.Text.Replace(":", "")
    TextBox1.SelectionStart = TextBox1.Text.Length
End Sub

TextBox1替换为您的文本框名称

答案 1 :(得分:1)

如果您链接到JQuery,您可以检查它是否为冒号,如果输入冒号则返回false:

<script type="text/javascript">
$(document).ready(function() {
    $('.phoneInput').keypress(function(key) {
       if(key.charCode ==58) return false;
    });
});
</script>

要查找角色的Unicode值,我倾向于在W3Schools上使用此页面:

http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_event_key_charcode

我希望这有帮助!

答案 2 :(得分:1)

您可以使用JavaScript禁用或阻止用户使用特殊字符。举个例子:

<head>
    $('input[type="text"]').keydown(function(e){
    var ingnore_key_codes = [8,32];

    if ($.inArray(e.keyCode, ingnore_key_codes) >= 0)
      {
          e.preventDefault();
      }
   });
</head

<form action="#">
     <input type="text" />
</form>

有关详情,请查看此内容 - http://blog.troygrosfield.com/2010/12/01/preventing-character-input-using-javascript/

答案 3 :(得分:0)

将其转换为ascii,58是58的

的ascii整数
  Private Sub txtchange() Handles TextBox1.Keypress
    if asc(e.keychar) = 58 Then
      e.handled = true
      Messagebox.show("Colon is not allowed")
    end if
   End Sub