简体   繁体   English

jQuery验证的自定义属性不起作用

[英]jQuery validation for custom attribute not working

I have created a custom validation attribute which checks to see if a string has the specified minimum length 我创建了一个自定义验证属性,该属性检查字符串是否具有指定的最小长度

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public sealed class MinimumLengthAttribute : ValidationAttribute
    {
        public int MinLength { get; set; }
        private static string _errorMessage = Messages.MinimumLengthError;

        public MinimumLengthAttribute(int minLength):base(_errorMessage)
        {
            MinLength = minLength;
        }

        public override string FormatErrorMessage(string name)
        {
            var message = String.Format(CultureInfo.CurrentUICulture, ErrorMessageString, name, MinLength);
            return message;
        }

        public override bool IsValid(object value)
        {
            if (value == null)
            {
                return true;
            }
            string valueAsString = value as string;
            return (valueAsString != null && valueAsString.Length >= MinLength);
        }
    }

    public class MinimumLengthValidator : DataAnnotationsModelValidator<MinimumLengthAttribute>
    {
        public MinimumLengthValidator(ModelMetadata metadata, ControllerContext context, MinimumLengthAttribute attribute)
            : base(metadata, context, attribute)
        {
        }

        public override IEnumerable<ModelClientValidationRule>GetClientValidationRules()
        {
            var rule = new ModelClientValidationRule
            {
                ErrorMessage = Attribute.ErrorMessage,
                ValidationType = "checkMinimumLength"
            };
            rule.ValidationParameters.Add("minimumLength", Attribute.MinLength);

            return new[] { rule };
        }
    }

Below is my model class 下面是我的模型课

public class SignUpViewModel
    {
        [Required]
        [StringLength(100)]
        public string Username { get; set; }

        [Required]
        [MinimumLength(6)]
        public string Password { get; set; }
    }

I have a js file which contains the client side validation logic as follows: 我有一个包含客户端验证逻辑的js文件,如下所示:

jQuery.validator.addMethod("checkMinimumLength", function (value, element, params) {
    if (this.optional(element)) {
        return true;
    }

    if (value > params.minimumLength) {
        return true;
    }

    return false;
});

I have also registered my custom attribute in my global.asax file but for some reason when I tab out of the textbox I get '$.validator.methods[...]' is null or not an object error in the jquery.validate.js file. 我还在我的global.asax文件中注册了自定义属性,但是由于某些原因,当我跳出文本框时,我得到'$ .validator.methods [...]'为null或 jquery.validate中没有对象错误。 .js文件。 What am I doing wrong here? 我在这里做错了什么?

如果您使用的是包装的版本(jquery.validate.pack.js)。用缩小的版本(jquery.validate.min.js)JavaScript文件来代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM