4

I would like to know if a way exists in asp.net mvc 2, to have multiple regular expression on a proprety. For example :

[RegularExpression("[^0-9]", ErrorMessageResourceName = ValidationMessageResourceNames.OnlyDigits, ErrorMessageResourceType = typeof(ValidationMessages))]
[RegularExpression("[^<>]{2,}", ErrorMessageResourceName = ValidationMessageResourceNames.SpecialCharErrorCreateAccount, ErrorMessageResourceType = typeof(ValidationMessages))]
public string City { get; set; }

The target here, is two have two specific error messages, one for the digits and one other for the special Chars and the fact that the min lenght must be 2 chars.

Thanks in advance for the help, or experience.

Etienne.

Etienne
  • 41
  • 1
  • 2
  • I'm not sure I understand your requirements exactly. What are the rules for a string to fail/pass validation? Could you show some examples of string you do or don't want to match? – Tim Pietzcker Jan 26 '11 at 10:42

3 Answers3

5

Something like this:

    public class DigitsAttribute : RegularExpressionAttribute
    {
        public DigitsAttribute()
            : base("[^0-9]")
        {
        }    
    }

    public class SpecialCharsAttribute : RegularExpressionAttribute
    {
        public SpecialCharsAttribute()
            : base("[^<>]{2,}")
        {
        }
    }

Use:

[Digits]
[SpecialChars]
public string City { get; set; }
frennky
  • 12,581
  • 10
  • 47
  • 63
  • can u plz have a look at this question http://stackoverflow.com/questions/12567194/custom-model-validation-is-not-firing-in-asp-net-mvc3 – IT ppl Sep 25 '12 at 13:19
  • Will this use client validation out-of-the-box or not? EDIT: Now I see that it doesn't.. :( – user2173353 Jul 10 '14 at 10:35
1

Change attribute usage with a custom class

The best solution is of course to create a custom attribute that inherits from RegularExpressionAttribute but sets different attribute usage settings. The main setting is AllowMultiple that you need to set to true.

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

You would use this attribute just like you use the existing RegularExpressionAttribute, but you'd have the ability to put multiple of them on the same property.

Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
  • Can you show what exactly goes into the class? I really wanted to use this method but I am fairly new to this. The isValid method in the new class somehow never got visit and I have no idea why. – Jacky Cheng Sep 10 '15 at 03:22
  • 1
    Do not use this solution,see this http://stackoverflow.com/questions/35652533/should-i-use-multiple-regularexpression-attributes/35658502#35658502 – George Vovos Feb 26 '16 at 17:55
-1

Try this,

[RegularExpression("[^0-9]|[^<>]{2,}", ErrorMessageResourceName = ValidationMessageResourceNames.OnlyDigits, ErrorMessageResourceType = typeof(ValidationMessages))]
public string City { get; set; }

Here '|' has been used as OR condition to match

amiry jd
  • 27,021
  • 30
  • 116
  • 215
Sohel Pathan
  • 367
  • 3
  • 13
  • I'm interested in the downvote. This _should_ be the way it works, though I'm having some trouble with .net Core… – Auspex May 25 '22 at 15:17