Thursday 15 May 2014

.net - asp.net regular expression validator client side script error -


I have to find out that the input string has HTML / script tags and if there is such a valid error, then the following regular expression The verifier is:

  & lt; Asp: text box id = "txt" runat = "server" /> & Lt; Asp: RegularExpressionValidator ControlToValidate = "txt" runat = "server" id = "regexVal" EnableClientScript = "true" display = "dynamic" error message = "invalid content" text = "!" Validation expression = "& gt; (?: (? & Lt; t & gt; [^   

When I run this markup hosting page, I get a secret error with the message "Syntax error in regular expression". However, when I take the same regex and it uses the Reggae class from the system. Lesson Regular expression works just fine: like:

  Regex r = new Regex ("> (? :(? & Lt; t & gt; [^ & lt;] * )) "); R.IsMatch (@ "& lt; b & gt; This should be a validation error & lt; / b & gt;"); R.IsMatch ("It's OK");  

What am I missing

Update: WebResource.axd is having an error in the following JS function:

  function Regular Expression ViaductAdvanced ISValid (Val) {var value = Value Eatergate Value (Val Contralolidate); If (ValidateTrem (value) length == 0) true return; Var rx = new regExp (val.validationexpression); // This error occurs with error var matches = rx.exec (value); Return (matches! = Null & value == matches [0]); } I think the problem is that Javascript understands the regular syntax of the syntax. Does not come in. Grouping  

When you set EnableClientScript to true to RegularExpressionValidator makes ASP.NET your regular expression in JavaScript Control in order to enable client-side validation on you. In this case, javascript has named named groups (? ? ... <... <... code) and non-capturing group (? : ...) is not supported for syntax. Although these features are working in NAT JavaScript, it is struggling with them.

From:

On the client, the JScript Regular Expression syntax is used. On the server, the Regex syntax is used. Because JScript is a subset of regular expression syntax syntax syntax, it is recommended that you use the JScript regular expression syntax to generate the same result on both the client and the server.

There are two ways that you can improve it:

  1. Disable the client-side script generation and perform regular explorations on the server-side. You can do this by setting EnableClientScript to false .
  2. Modify the regular expression and remove the non-capturing group and named groups. If you need to capture in your regular expression, then (...) syntax should work correctly in both Javascript and .NET. You will then use the reference number in the sequence number used for the captured values ​​( $ 1 , $ 2 , etc.). [^ & Lt;] * See how the purpose should work.

I want to point out some other issues:

  • You do not have to capture the original regular expression if you want to , Then examine the existence of all the opening brackets. It has & gt; [^ & Lt;] * can be rewritten which will be simple and will work in the same way. It will not capture any value in the original string, but since you are using it in ASP.NET Verification control, it will not make any difference.
  • The way you are implementing , RegularExpressionValidator will only work if the match is successful in your case, you will be verified if your code is in the Gt; Blah Something I think is that you want to work it in another way.
  • If you select & gt; Regular expression in [^ & lt;] * , then regular expressions will still not work. I think you want to intend that the verification control attempts to match all the text in the text box . So if I type the & gt; Blah , then this match will be, but and lt; B & gt; Blah & lt; / B & gt; Because the regular expression says that string is a & gt; should begin with & gt; to allow text before . * & Gt ;. * [^ & Lt;] * would like to suggest something.

No comments:

Post a Comment