的多个属性类型=“电子邮件”不适用于 ASP.Net-Core

发布于 2025-01-14 19:44:13 字数 644 浏览 4 评论 0原文

在 ASP 下,我们可以将控件与具有成员的模型绑定

   public string Contact { get; set; }

,或者直接 或通过相应的 HTML helper

我们也可以使用数据注释而不是在 Razor 页面中隐式声明类型

   [EmailAddress]
   public string Contact { get; set; }   

但是,如果我想输入以逗号分隔的电子邮件地址列表,该怎么办?

无界 HTML5 代码 在最新浏览器下工作是正确的: type="email" 的多个属性不起作用。但是,当我尝试将其绑定到模型时,看起来 EmailAddressAttribute 已应用于模型,并且只能验证一个电子邮件地址

Under ASP we could bound a control with a model which has member

   public string Contact { get; set; }

or directly <input type="email" asp-for="item.Contact"> or through corresponding HTML helper

As well we could use Data Annotation instead of implicitly declare type in Razor page

   [EmailAddress]
   public string Contact { get; set; }   

But what to do if I would like to enter the list of email addresses separated by comma?

It is correct that unbounded HTML5 code <input type="email" multiple> works under latest browsers:
Multiple attribute for type="email" does not work. But when I am trying to bound it to the model it looks like EmailAddressAttribute is applied to the model and only one email address could be validated

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

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

发布评论

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

评论(1

聚集的泪 2025-01-21 19:44:13

就像@pcalkins所说,浏览器不会为你分离它,你必须实现一些拆分电子邮件功能,如下所示

// 1. put this helper in your utilty class
private IEnumerable<string> GetEmails(string input)
{
    if (String.IsNullOrWhiteSpace(input)) yield break;
    MatchCollection matches = Regex.Matches(input, @"[^\s<]+@[^\s,>]+");
    foreach (Match match in matches) yield return match.Value;
}

// 2. now call it to get list of emails
// for e.g. string strEmails = "Last, First <[email protected]>, [email protected], First Last <[email protected]>..";
string allContactsWithCommas = model.contactsWithCommas;
IEnumerable<string> emails = GetEmails(allContactsWithCommas );

// 3. try to give it something custom to validate
//[Required, MinLength(1, ErrorMessage = "Some validation error")]
[YourClassHoldingObject]
public List<int> Contact { get; set; }

// 4. or implement something custom in for your validation object, so the broswer knows how to handle/waht to call for validation
public class YourClassHoldingObject : IValidatableObject
{
    [Required]
    List<int> Contact 

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
    // your contact logic validation here
        if (Contact.Count < 1)
        {

            // some validation using System.ComponentModel.DataAnnotations
            // - please customize for your needs
            yield return new ValidationResult(
                $"At least one email should be specified.", new[] 
                { System.ComponentModel.DataAnnotations.EmailAddressAttribute().IsValid(
                // for e.g. "[email protected]") });
                Contact) });
        }
    }   
}

Like @pcalkins said, the browsers will not separate it for you, you have to implement some split emails functionality like so

// 1. put this helper in your utilty class
private IEnumerable<string> GetEmails(string input)
{
    if (String.IsNullOrWhiteSpace(input)) yield break;
    MatchCollection matches = Regex.Matches(input, @"[^\s<]+@[^\s,>]+");
    foreach (Match match in matches) yield return match.Value;
}

// 2. now call it to get list of emails
// for e.g. string strEmails = "Last, First <[email protected]>, [email protected], First Last <[email protected]>..";
string allContactsWithCommas = model.contactsWithCommas;
IEnumerable<string> emails = GetEmails(allContactsWithCommas );

// 3. try to give it something custom to validate
//[Required, MinLength(1, ErrorMessage = "Some validation error")]
[YourClassHoldingObject]
public List<int> Contact { get; set; }

// 4. or implement something custom in for your validation object, so the broswer knows how to handle/waht to call for validation
public class YourClassHoldingObject : IValidatableObject
{
    [Required]
    List<int> Contact 

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
    // your contact logic validation here
        if (Contact.Count < 1)
        {

            // some validation using System.ComponentModel.DataAnnotations
            // - please customize for your needs
            yield return new ValidationResult(
                
quot;At least one email should be specified.", new[] 
                { System.ComponentModel.DataAnnotations.EmailAddressAttribute().IsValid(
                // for e.g. "[email protected]") });
                Contact) });
        }
    }   
}

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