1

I am trying to use a RegexStringValidator on a couple of properties:

[Required, StringLength(4, MinimumLength = 4), RegexStringValidator(@"AAAA")]
public virtual string ValueGuid { get; set; }

This actually accepts anything that is 4 characters long and not just "AAAA" which I would expect. Is there some magic I'm missing here?

Thanks Anton

1 Answer 1

4

You need to use the RegularExpressionAttribute from the data annotations namespace and not the RegexStringValidator.

The RegexStringValidator is for validation of custom configuration properties and is not picked up for validation by the default MVC model binder.

The supported validation attributes located in the data annotations namespace are the following:

  • Range – Enables you to validate whether the value of a property falls between a specified range of values.
  • ReqularExpression – Enables you to validate whether the value of a property matches a specified regular expression pattern.
  • Required – Enables you to mark a property as required.
  • StringLength – Enables you to specify a maximum length for a string property.

There also two additional attributes supplied by MVC framework:

  • Remote - Enables to perform client-side validation with a server callback.
  • Compare - Ensures that two properties in a model object have the same value.
Sign up to request clarification or add additional context in comments.

1 Comment

That would explain it! Thanks very much, it's now working as expected. I have no idea where I got that class from...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.