0

jQuery

$(document).ready(function () {
    var EmailContainer = $('#EmailContainer');
    var url = EmailContainer.attr('data-url');
    EmailContainer.load(url, function () {
        var Emailform = $('#EmailForm');
        $.validator.unobtrusive.parse(Emailform);
        Emailform.submit(function () {
            debugger;
            var Emailform = $(this);
            if (Emailform.valid()) {
                var SendEmailUrl = EmailContainer.attr('Json-Url');
                var UserModel = { From: $('#From').val(), To: $('#To').val(), 
                      Subject: $('#Subject').val(), 
                      Description: $('#Description').val() }
                $.post(SendEmailUrl, UserModel, function (data) {
                    Emailform.html(data);
                    Emailform.removeData('validator');
                    Emailform.removeData('unobtrusiveValidation');
                    $.validator.unobtrusive.parse(Emailform);
                });
            }
            return false;
        });
    });
});

模型

public class User
    {
        [Required(ErrorMessageResourceType = typeof(Resource1), 
              ErrorMessageResourceName = "From")]
        [DataType(System.ComponentModel.DataAnnotations.DataType.EmailAddress,
              ErrorMessageResourceType = typeof(Resource1), 
              ErrorMessageResourceName = "InvalidEmail")]
        [DisplayName("From Email Address")]
        public String From { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resource1),
                  ErrorMessageResourceName = "To")]
        [DataType(System.ComponentModel.DataAnnotations.DataType.EmailAddress, 
                  ErrorMessageResourceType = typeof(Resource1), 
                  ErrorMessageResourceName = "InvalidEmail")]
        [DisplayName("To Email Address")]
        public String To { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resource1),
                  ErrorMessageResourceName = "Subject")]
        [DisplayName("Subject")]
        public String Subject { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resource1),
                  ErrorMessageResourceName = "Description")]
        [DisplayName("Description")]
        public String Description { get; set; }
    }

问题 - 不显示无效电子邮件地址的验证

4

2 回答 2

2

问题 - 不显示无效电子邮件地址的验证

我在您的代码中看不到您进行任何验证的任何地方。如果要进行验证,请在模型属性上使用相应的验证属性:

[Required(ErrorMessageResourceType = typeof(Resource1), 
      ErrorMessageResourceName = "From")]
[DataType(System.ComponentModel.DataAnnotations.DataType.EmailAddress,
      ErrorMessageResourceType = typeof(Resource1), 
      ErrorMessageResourceName = "InvalidEmail")]
[DisplayName("From Email Address")]
[RegularExpression("GET A REGEX TO VALIDATE AN EMAIL FROM HERE: http://www.regular-expressions.info/email.html")]
public string From { get; set; }

To如果这是一封电子邮件,显然这同样适用于您的财产:

[Required(ErrorMessageResourceType = typeof(Resource1),
          ErrorMessageResourceName = "To")]
[DataType(System.ComponentModel.DataAnnotations.DataType.EmailAddress, 
          ErrorMessageResourceType = typeof(Resource1), 
          ErrorMessageResourceName = "InvalidEmail")]
[DisplayName("To Email Address")]
[RegularExpression("GET A REGEX TO VALIDATE AN EMAIL FROM HERE: http://www.regular-expressions.info/email.html")]
public string To { get; set; }

因此,请在此处获取电子邮件的正则表达式:http ://www.regular-expressions.info/email.html

于 2013-02-19T11:21:53.773 回答
-1

System.ComponentModel.DataAnnotations.DataType.EmailAddress不是用于验证电子邮件。它用于 ASP.Net MVC 可以决定如何显示字段的值。

您必须编写自己的电子邮件验证逻辑。如果我们应用于System.ComponentModel.DataAnnotations.DataType.EmailAddress任何属性,则该字段将显示为超链接,而不是作为普通文本显示,该文本将添加一个“mailTo”链接。通过编写自定义数据注释添加您自己的验证电子邮件地址的逻辑。

干杯

于 2013-02-19T11:08:29.933 回答