0

If use default attribte, then validation work on server and browser

[StringLength(100, MinimumLength = 6, ErrorMessageResourceName = "StrLenMinMax", ErrorMessageResourceType = typeof(Resources.Resources))]
public string Password { get; set; }

But if use custom attribute validation work only on server (validation on the page is only through an error by VIEW)

I am not have runtime validation, only after submit

[StringLengthEx(100, MinimumLength = 6)]
public string Password { get; set; }

public class StringLengthExAttribute : StringLengthAttribute
    {
        private const string StringLengthMin = "StrLenMin";
        private const string StringLengthMinMax = "StrLenMinMax";

        public StringLengthExAttribute(int maxLength):base(maxLength)
        {
            ErrorMessageResourceType = typeof(Resources.Resources);
            if (MinimumLength != 0)
            {
                MinimumLength = MinimumLength;
                ErrorMessageResourceName = StringLengthMinMax;
            }
            else
            {
                ErrorMessageResourceName = StringLengthMin;
            }

        }
    }
  • This is because client side validation (unObtrusive) translates the validator to JavaScript, and uses it in client side. Take a look into the page source.. If's it's a new attribute, - it doesn't know how to translate that to JS – Marty Mar 16 '14 at 09:39
  • this is not new attribute this polymorphism – user3356386 Mar 16 '14 at 09:42
  • @user3356386 Maybe you need to learn english better, but subclassing IS creating a new attibute. Whatever geenrates the javascript does not know the attbribute, so it can not generate javascript. – TomTom Mar 16 '14 at 09:53

1 Answers1

0

here's a detailed answer to Your question Perform client side validation for custom attribute

or you can just google for more https://www.google.lt/search?q=mvc+client+side+validation+custom+attribute&oq=mvc+client+side+validation+custom+attribute

Community
  • 1
  • 1
Marty
  • 3,485
  • 8
  • 38
  • 69