2

I have this attribute in my view model:

    [CustomRequired, EmailRegex]
    [Display(Name = "KeepInformedPersonMail", ResourceType = typeof (UserResource))]
    public string Email { get; set; }

The EmailRegex is like this:

public class EmailRegexAttribute : RegularExpressionAttribute
{
    private const string EmailPattern =
        @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";

    public EmailRegexAttribute()
        : base(EmailPattern)
    {
        ErrorMessageResourceType = typeof(UserResource);
        ErrorMessageResourceName = "InvalidEmail";
    }
}

It works for server side validation but no client side.

If I replace the EmailRegex with the following it works both client and server side validation:

    [CustomRequired]
    [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", ErrorMessage = "Please enter a valid e-mail adress")] 
    [Display(Name = "KeepInformedPersonMail", ResourceType = typeof (UserResource))]
    public string Email { get; set; }

Does someone can explain me how can I proceed to have client and server side validation for my initial EmailRegex?

Thanks.

Bronzato
  • 9,438
  • 29
  • 120
  • 212

1 Answers1

0

This answer should solve your problem

Community
  • 1
  • 1
Iridio
  • 9,213
  • 4
  • 49
  • 71