I am creating custom email validation attribute, my code :
public class EmailAttribute : RegularExpressionAttribute
{
public EmailAttribute()
: base("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2,4})$")
{ }
}
Now i put this attribute on an email property like :
[Required(ErrorMessage = "Required")]
[Email(ErrorMessage = "Must be a valid Email")]
public string Email { get; set; }
But this is not validating the email, Required attribute works fine but Email is not working. Can anybody please explain me why?
The server-side validation should work fine. I think you are talking about the client side validation and for that you have to
IClientValidatableinterface in the custom email attribute and also you have to create jquery adapter.You can see an example here.