在验证电子邮件地址时遇到问题

时间:2013-02-19 11:03:23

标签: jquery asp.net-mvc asp.net-mvc-3 unobtrusive-validation

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

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:-1)

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

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

干杯