English 中文(简体)
ASP MVC 2 Validation: Passing Javascript Code to the/67/
原标题:ASP MVC 2 Validation : Passing Javascript code to the client

I am writing a custom validation attribute It does conditional validation between two fields When I create my rule, one of the things that I could not solve is how to pass javascript code through ValidationParameters

Usually, I just do ValidationParameters["Param1"] = "{ required :function(element) { return $("#age").val() < 13;) }"

但是,微软MvcJ QueryValidation.js 例行这样做,以达到这一目的。

第1段=“所需功能:功能(部分){回报(“#age”)和“t”;13;}”

我可以在 Java稿中使用Param1.eval()。 这将评价和执行该守则,但我只是想对守则进行估价,然后执行。

JSON parser不穿透Javascript

因此,我在此要求任何想法。

问题回答

不知道你会怎样做描述,但你可能想考虑使用的习俗验证模式。

重要部分是ValidationAttribute、数据发布 验证员,在申请中登记验证员,登记数据发布员ModelValidatorProvider.RegisterAdapter和客户方Sys.Mvc.ValidatorRegistry.validators,以登记客户方验证代码。

这里引用我职务的榜样。

[RegularExpression("[\S]{6,}", ErrorMessage = "Must be at least 6 characters.")]
public string Password { get; set; }


[StringLength(128, ErrorMessage = "Must be under 128 characters.")]
[MinStringLength(3, ErrorMessage = "Must be at least 3 characters.")]
public string PasswordAnswer { get; set; }


public class MinStringLengthAttribute : ValidationAttribute
{
  public int MinLength { get; set; }

  public MinStringLengthAttribute(int minLength)
  {
    MinLength = minLength;
  }

  public override bool IsValid(object value)
  {
    if (null == value) return true;     //not a required validator
    var len = value.ToString().Length;
    if (len < MinLength)
      return false;
    else
      return true;
  }
}

public class MinStringLengthValidator : DataAnnotationsModelValidator<MinStringLengthAttribute>
{
  int minLength;
  string message;

  public MinStringLengthValidator(ModelMetadata metadata, ControllerContext context, MinStringLengthAttribute attribute)
    : base(metadata, context, attribute)
  {
    minLength = attribute.MinLength;
    message = attribute.ErrorMessage;
  }

  public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
  {
    var rule = new ModelClientValidationRule
    {
      ErrorMessage = message,
      ValidationType = "minlen"
    };
    rule.ValidationParameters.Add("min", minLength);
    return new[] { rule };
  }
}


protected void Application_Start()
{
  RegisterRoutes(RouteTable.Routes);
  DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(MinStringLengthAttribute), typeof(MinStringLengthValidator));
}


Sys.Mvc.ValidatorRegistry.validators["minlen"] = function(rule) {
  //initialization
  var minLen = rule.ValidationParameters["min"];

  //return validator function
  return function(value, context) {
    if (value.length < minLen) 
      return rule.ErrorMessage;
    else
      return true;  /* success */
  };
};




相关问题
Bind Button.IsEnabled to custom validation with XAML?

I am sorry I didn t know how to title my question any better, you name it if you got a good 1. I have an entity Contact. this person has navigation properties: Address, Phones (A collection of Phone)....

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

Wpf Combobox Limit to List

We are using Wpf Combobox to allow the user to do the following things: 1) select items by typing in the first few characters 2) auto complete the entry by filtering the list 3) suggesting the first ...

Rails 101 | validates_currency?

I ve searched high and low, but I could not find a solution, to what I think seems like a very common task. In a form I want to have a text input that accepts currency strings (i.e. $1,000,000 or ...

CodeIgniter form verification and class

I m using the form validation library and have something like this in the view <p> <label for="NAME">Name <span class="required">*</span></label> <?...

热门标签