2

I have a problem with the validation message for Regular expression. Localised messages appear everywhere except for one field below:

    [LocalizedDisplayName("LblWordCount", NameResourceType = typeof(ValidationMessages.Messages))]
    [Required(ErrorMessageResourceName = "ErrorFieldRequired", ErrorMessageResourceType = typeof(ValidationMessages.Messages))]
    [RegularExpression(@"^[0-9]+$", ErrorMessage = "", ErrorMessageResourceName = "ErrorDigitsOnly", ErrorMessageResourceType = typeof(ValidationMessages.Messages))]
    public Int32 WordCount { get; set; }

It doesn't matter what I put in the resx file for "ErrorDigitsOnly" - the default message always kicks in: "The value 'zxzza1' is not valid for Word Count." For instance - message for the [Required] appears correctly.

Any suggestions for that?

Cheers, 303

I have checked the code for spelling mistakes but couldn't find any.

1
  • Remove ErrorMessage = "" from the attribute worked for me Commented Aug 28, 2012 at 17:06

1 Answer 1

3

I actually ran across a similar situation myself. Have you tried setting the wordcount validation in another class instead of using the regex pattern in the DataAnnotation Attribute?

For example -

  public class EmailAttribute: RegularExpressionAttribute
{
    public EmailAttribute() : base(@"[^@\.]+(\.[^@\.]+)*@[^@\.]+(\.[^@\.]+)?(\.[^@\.]{2,})")
    {
    }

    public override string FormatErrorMessage(string name)
    {
        return Resources.Resources.emailValidation;
    }
}

You can then use the attribute like so -

    [CustomRequired]
    [Email]
    public string Email { get; set; }

The advantages of using it this way means that you get strong typing on your resources and it also allows you to write neater validation classes.

Hope it helps!

Sign up to request clarification or add additional context in comments.

Comments

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.