I am using the class below and the default scaffold view, but I am unable to validate email on client side. I also tried another email regular expression but that didn’t work either. What am I missing here?
public class Person
{
[Email(ErrorMessage="...........")]
public string Name { get; set; }
}
public class EmailAttribute : RegularExpressionAttribute
{
public EmailAttribute()
: base(@"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$")
{
}
}
You might find the following blog post useful. Taken from it:
And the usage:
And that’s pretty much all. Because this custom attribute implements IClientValidatable the client validation will work as well.
And of course if the regular expression used in this example doesn’t suits you don’t hesitate to improve it.