【问题标题】:How to allow only English (A-Z) characters to be typed into a textbox?如何只允许在文本框中输入英文(A-Z)字符?
【发布时间】:2011-01-30 09:05:51
【问题描述】:

我想只允许在我的asp:TextBox 中输入英文字符(在asp:Login 控件中)。

我该怎么做?

【问题讨论】:

  • 您希望将以下哪个算作“英文字符”:é 为 lamé,ç 为 soupçon,ï 为 naïve,ô 为 rôle...
  • @AakashM:我不希望用户在登录或密码字段中输入“Вася Пупкин” :)

标签: .net asp.net textbox passwords


【解决方案1】:

如果你想验证输入

使用RegularExpressionValidator 控件。 How To: Use Regular Expressions to Constrain Input in ASP.NET 有一个不错的示例和解释。

仅英文(大写和小写 A-Z)字符的正则表达式为 ^[A-Za-z]*$

如果你也想包含数字,^[A-Za-z0-9]*$

您要添加的标记片段:

<asp:RegularExpressionValidator ID="regexpName" runat="server"     
      ErrorMessage="Only upper and lower case A-Z may be entered here." 
      ControlToValidate="txtInput"     
      ValidationExpression="^[A-Za-z]*$" />

如果您想阻止用户在框中输入其他内容

你最好在这个问题的答案中使用 jQuery 脚本: how do i block or restrict special characters from input fields with jquery?

但是您也总是想要验证输入(例如,如果用户关闭 JavaScript 会怎样),所以也要使用上述验证方法。

【讨论】:

    【解决方案2】:

    您可以使用FilteredTextBox 实现此目的。

    也可以通过注册一个 onKeyUp 侦听器,并过滤掉不在允许子集中的字符(也许用正则表达式替换)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-31
      • 2016-06-01
      • 2019-02-10
      • 1970-01-01
      • 2013-04-12
      • 1970-01-01
      相关资源
      最近更新 更多