开发者

Validating an e-mail address with unobtrusive javascript / MVC3 and DataAnnotations

开发者 https://www.devze.com 2023-03-09 05:38 出处:网络
jQuery Validation makes it simple to validate an email address: $(\"someForm\").validate({ rules: { SomeField: {

jQuery Validation makes it simple to validate an email address:

$("someForm").validate({
    rules: {
        SomeField: {
            required: true,
            email: true,
            remote: {
                type: "POST",
                url: "CheckEmail"
            }
        }
    }
});

This makes it so that SomeField is required, must be formatted as an e-mail address and also performs a remote call to the CheckEmail action (check for duplicates).

I like to make things as simple as possible so I can do a lot of the same stuff with Data Annotations:

public class RegisterModel {
    [Required]
    [Remote("CheckEmail", "Home", HttpMethod="POST")]
    public string SomeField { get; set; }
}

Does ASP.net MVC 3 / Data Annotations h开发者_开发知识库ave a built-in/simple way to validate to make sure the e-mail address is in the correct format?

I would like it to produce unobtrusive javascript if possible.


I think this is the code you are looking for (this is similar to ScottGu's example but also shows the DisplayName in the default error message instead of the property name):

public class EmailAttribute : RegularExpressionAttribute
{
    private const string defaultErrorMessage = "'{0}' must be a valid email address";

    public EmailAttribute() : 
        base("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9]+(\\.[a-z0-9]+)*\\.([a-z]{2,4})$")
    { }

    public override string FormatErrorMessage(string name)
    {
        return string.Format(defaultErrorMessage, name);
    }

    protected override ValidationResult IsValid(object value,
                                            ValidationContext validationContext)
    {
        if (value != null)
        {
            if (!base.IsValid(value))
            {
                return new ValidationResult(
                    FormatErrorMessage(validationContext.DisplayName));
            }
        }

        return ValidationResult.Success;
    }
}

Then your model property would look like this:

    [DisplayName("My Email Address")]
    [Email]
    public string EmailAddress { get; set; }


Does ASP.net MVC 3 / Data Annotations have a built-in/simple way to validate to make sure the e-mail address is in the correct format?

Not built-in but you could use a [RegularExpression]. Scott Gu illustrated an example of such regex in a blog post. He wrote a custom EmailAttribute deriving from RegularExpressionAttribute to avoid repeating logic.


The Data Annotation Extensions library has an [Email] attribute that allows for validating an email address.

There is also a blog post outlining how to use the library.


EmailAddress attribute is built into framework already, no need for Data Annotation Extensions or other logic: Email model validation with DataAnnotations and DataType

0

精彩评论

暂无评论...
验证码 换一张
取 消