开发者

jQuery validation for custom attribute not working

开发者 https://www.devze.com 2023-01-23 15:54 出处:网络
I have created a custom validation attribute which checks to see if a string has the specified minimum 开发者_StackOverflow社区length

I have created a custom validation attribute which checks to see if a string has the specified minimum 开发者_StackOverflow社区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:

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. What am I doing wrong here?


if you are using the packed version (jquery.validate.pack.js).use the minified version (jquery.validate.min.js) javascript file instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号