I have the following regular expression validator to detect whether an input string contains HTML/script tags and if so cause a vaidation error:
<asp:TextBox ID="txt" runat="server" />
<asp:RegularExpressionValidator
ControlToValidate="txt"
runat="server"
ID="regexVal"
EnableClientScript="true" Display="Dynamic"
ErrorMessage="Invalid Content"
Text="!"
ValidationExpression=">(?:(?<t>[^<]*))" />
When I run the page hosting this markup I get a scipt error with the message "Syntax Error in Regular Expression". However when I take the same regex and run it using Regex class from System.Text.RegularExpressions everything works fine: Like so:
Regex r = new Regex(">(?:(?<t>[^<]*))");
r.IsMatch(@"<b>This should cause a validation error</b>");
r.IsMatch("this is fine");
What am I missing
UPDATE: The error seems to be happening in the following js function in WebResource.axd:
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]);
}