我有以下正则表达式验证器,用于检测输入字符串是否包含HTML/script标记,如果是,则会导致验证错误:
当我运行包含此标记的页面时,出现脚本错误,错误信息为“正则表达式语法错误”。 然而,当我使用System.Text.RegularExpressions中的Regex类运行相同的正则表达式时,一切都正常: 像这样:
<asp:TextBox ID="txt" runat="server" />
<asp:RegularExpressionValidator
ControlToValidate="txt"
runat="server"
ID="regexVal"
EnableClientScript="true" Display="Dynamic"
ErrorMessage="Invalid Content"
Text="!"
ValidationExpression=">(?:(?<t>[^<]*))" />
当我运行包含此标记的页面时,出现脚本错误,错误信息为“正则表达式语法错误”。 然而,当我使用System.Text.RegularExpressions中的Regex类运行相同的正则表达式时,一切都正常: 像这样:
Regex r = new Regex(">(?:(?<t>[^<]*))");
r.IsMatch(@"<b>This should cause a validation error</b>");
r.IsMatch("this is fine");
我错过了什么?
更新: 错误似乎发生在WebResource.axd中以下js函数中:
function RegularExpressionValidatorEvaluateIsValid(val) {
var value = ValidatorGetValue(val.controltovalidate);
if (ValidatorTrim(value).length == 0)
return true;
var rx = new RegExp(val.validationexpression); //this is the line causing the error
var matches = rx.exec(value);
return (matches != null && value == matches[0]);
}
>(?:([^<]*))
似乎也不起作用。你测试过吗? - o.k.w