无法在客户端自定义验证函数中获取值

本文关键字:函数 获取 验证 自定义 客户端 | 更新日期: 2023-09-27 18:25:35

我正在使用jQuery进行客户端自定义验证,我一直在从服务器端在客户端执行操作。。。。这是我的服务器端自定义验证功能

public class SelctedValueCheckAttribute : ValidationAttribute , IClientValidatable
{
    public SelctedValueCheckAttribute(string otherProperty): base("{0} is not in correct range")
    {
        OtherProperty = otherProperty;
    }
    public string OtherProperty { get; set; }
    public SelctedValueCheckAttribute()
    {
        ErrorMessage = "Values must be in the Given Range";
    }
    public override string FormatErrorMessage(string name)
    {
        return "The Entered Value Must be in given range for " + name + "item";
    }
    protected override ValidationResult IsValid(object firstValue, ValidationContext validationContext)
    {
        string selecetdItemValue = firstValue as string ;
        string userEnteredValue = GetSecondValue(validationContext);
        if( string.Equals(selecetdItemValue, "Amount"))
        {
             int entry = Convert.ToInt32(userEnteredValue);
             if (entry < 10 || entry > 20)
             {
                 return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
             }                                
        }
        else if (string.Equals(selecetdItemValue, "Pound"))
        {
            int entry = Convert.ToInt32(userEnteredValue);
            if (entry < 80 || entry > 90)
            {
                return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
            }
        }
        else if (string.Equals(selecetdItemValue, "Percent"))
        {
            int entry = Convert.ToInt32(userEnteredValue);
            if (entry < 50 || entry > 60)
            {
                return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
            }
        }
        return ValidationResult.Success;
    }
    protected string GetSecondValue(ValidationContext validationContext)
    {
      var propertyInfo = validationContext.ObjectType.GetProperty(OtherProperty);
      if (propertyInfo != null)
      {
       var secondValue = propertyInfo.GetValue(validationContext.ObjectInstance, null);
       return secondValue as string;
      }
      return null;
   }               
   public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        ModelClientValidationRule mcvr = new ModelClientValidationRule();
        mcvr.ValidationType = "enteredvaluescheck";
        mcvr.ErrorMessage = "Selected Value must be in given range";
        mcvr.ValidationParameters.Add("other", OtherProperty);
        mcvr.ValidationType = "selectedvaluewithenteredvaluecheck";
        yield return mcvr;
    }
}

这是我的客户端自定义验证

           jquery.validator.unobtrusive.adapters.addSingleval("selectedvaluewithenteredvaluecheck",  "other");
jQuery.validator.addMethod("selectedvaluewithenteredvaluecheck",
                           function(val,element,other)
                           {
                               if(val & other)
                               {
                                 // here I am not getting the values..
                                 //do I need to write any function to get the values
                                //is there any other approach that I need to follow
                               }

如何获取客户端函数中的值?有人对此有什么想法和建议吗?请建议我。

无法在客户端自定义验证函数中获取值

您错过了拼写为jqueryaddSingleval的单词。它们应该大写为QV

jQuery.validator.unobtrusive.adapters.addSingleVal("selectedvaluewithenteredvaluecheck",  "other");