【问题标题】:Regular Expression fails in IE, but works in Chrome and Firefox?正则表达式在 IE 中失败,但在 Chrome 和 Firefox 中有效?
【发布时间】:2010-07-14 05:28:23
【问题描述】:

我有以下 asp.net 标记:

<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"   
ValidationGroup="passwordValidation"></asp:TextBox>

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Display="Dynamic"
ControlToValidate="txtPassword" Text="Required" ValidationGroup="passwordValidation" />

<asp:RegularExpressionValidator runat="server" ControlToValidate="txtPassword"  
Text="Passwords should contain a minimum of 7 characters with at least one numeric 
character." ValidationExpression="^(?=.*\d{1})(?=.*[a-zA-Z]{2}).{7,}$"  
ValidationGroup="passwordValidation" Display="Dynamic"></asp:RegularExpressionValidator>

如果我输入像 test1234 这样的密码,它会在 chrome 和 firefox 中通过,但 Internet Explorer 中会显示我的密码应包含至少 7 个字符和至少一个数字字符的消息

【问题讨论】:

  • 我在IE8上用VS2010测试了上面的代码,无法重现问题。你能提供更多细节吗?
  • ...特别是,您在哪个版本的 IE 上测试它?

标签: asp.net regex


【解决方案1】:

你可能被臭名昭著的IE regex lookahead bug 咬了。您应该能够通过像其他条件一样将长度检查作为前瞻并将其放在首位来解决此问题。

^(?=.{7,}$)(?=.*\d)(?=.*[a-zA-Z]{2}).*

但我想我看到了另一个问题。 (?=.*[a-zA-Z]{2}) 匹配两个连续字母;这真的是你的意图吗?如果你想要求至少两个字母,但不一定是连续的,你应该使用(?=.*[a-zA-Z].*[a-zA-Z])

【讨论】:

  • 我会试一试,让你知道。我正在阅读有关它的帖子。我是正则表达式的新手,所以前瞻是我的头。
  • 如果你还没有,你可能想看看这个网站:regular-expressions.info
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-16
  • 1970-01-01
  • 2022-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多