AndreyAkinshin / knockout-mvc

Power of Knockout.js for ASP.NET MVC
http://knockoutmvc.com
203 stars 125 forks source link

Cannot use Regex in Computed observable #28

Open momoski opened 11 years ago

momoski commented 11 years ago

I have a computed property in my viewmodel like this:

  [Computed]
  public int PasswordScore
  {
      get
      {
          var strongRegex = new Regex("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$");
          var mediumRegex =
              new Regex("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$");
          //var enoughRegex = new Regex("(?=.{6,}).*");
          if (NewPassword.Length < 1)
              return 0;
          if (NewPassword .Length < 6)
              return 1;
          if (strongRegex.Match(NewPassword).Success)
              return 4;

          return mediumRegex.Match(NewPassword ).Success ? 3: 1;

      }
  }

Also I have in my View the following statement:

@ko.Html.Span(m => m.PasswordScore)

I get the error NotSupportedException. This exception happens in VisitMethodCall(MethodCallExpression m).

The state of the variables when reaching that function is:

?m.Method {System.Text.RegularExpressions.Match Match(System.String)} [System.Reflection.RuntimeMethodInfo]: {System.Text.RegularExpressions.Match Match(System.String)} base {System.Reflection.MethodBase}: {System.Text.RegularExpressions.Match Match(System.String)} MemberType: Method ReturnParameter: {System.Text.RegularExpressions.Match } ReturnType: {Name = "Match" FullName = "System.Text.RegularExpressions.Match"} ReturnTypeCustomAttributes: {System.Text.RegularExpressions.Match } ?m.Object {HelloWorldModel.expression} CanReduce: false DebugView: "KnockoutMvcDemo.Models.HelloWorldModel.expression" Expression: null Member: {System.Text.RegularExpressions.Regex expression} NodeType: MemberAccess Type: {Name = "Regex" FullName = "System.Text.RegularExpressions.Regex"}