1

My problem with MVC validation is that I have a lot of different condition where different message needs to be shown and there isn't enough default attribute type for me to put all those message in. I am aware that I could create custom attribute to solve this problem, but then I realize a lot of the validation I want could be done with simple regex checking, so instead of creating multiple different classes I tried to find a way to have multiple regexp attribute on a property.

After some research I found this SO post which suggest simply to inherit the RegularExpressionAttribute class, which is EXACTLY what I want, but I couldn't do the actual implementation myself as I am fairly new to .NET.

Here's the suggested code:

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple=true)]
public class MultiRegularExpressionAttribute: RegularExpressionAttribute
{
    ...
}

can anyone help me fill the actual content of the class out? I tried to fiddle around and it ran without error, but the isValid method never got visited and I am so very lost.

3
  • What do you mean have more error message variety? You can only display one message. Commented Sep 27, 2015 at 11:45
  • I have a lot of different condition where different message needs to be shown and there isn't enough attribute type for me to put all those message in, that's what i meant. Commented Sep 27, 2015 at 11:49
  • You cannot apply AllowMultiple=true to a validation attribute if your expecting to use client side validation. Commented Sep 27, 2015 at 11:53

0

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.