使用不显眼的 javascript / MVC3 和 DataAnnotations 验证电子邮件地址

发布于 2024-11-10 06:22:11 字数 739 浏览 0 评论 0原文

jQuery 验证使验证电子邮件地址变得简单:

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

这使得 SomeField 是必需的,必须格式化为电子邮件地址,并且还执行对 CheckEmail 操作的远程调用(检查重复项)。

我喜欢让事情尽可能简单,这样我就可以使用数据注释做很多相同的事情:

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

Does ASP.net MVC 3 / Data Annotations have abuilt-in/simple way to validate to make certain the e-mail address格式正确吗?

如果可能的话,我希望它能生成不引人注目的 JavaScript。

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 have 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

请持续率性 2024-11-17 06:22:11

我认为这是您正在寻找的代码(这与 ScottGu 的示例类似,但也在默认错误消息中显示 DisplayName 而不是属性名称):

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;
    }
}

那么您的模型属性将如下所示:

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

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; }
一笔一画续写前缘 2024-11-17 06:22:11

ASP.net MVC 3/数据注释吗
有一个内置/简单的方法来验证
确保电子邮件地址位于
正确的格式?

不是内置的,但您可以使用[RegularExpression]。 Scott Gu 在 中说明了此类正则表达式的示例博客文章。他编写了一个派生自 RegularExpressionAttribute 的自定义 EmailAttribute 以避免重复逻辑。

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.

戒ㄋ 2024-11-17 06:22:11

数据注释扩展库有一个[Email]属性,允许验证电子邮件。

还有一篇博客文章 概述如何使用该库。

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.

小傻瓜 2024-11-17 06:22:11

EmailAddress 属性已内置到框架中,不需要数据注释扩展或其他逻辑:使用 DataAnnotations 和 DataType 验证电子邮件模型

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文