SMC
SMC

Reputation: 237

Facing issue while validating the Email Address

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

Model

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

Issue - not showing validation for Invalid Email Address

Upvotes: 0

Views: 707

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038780

Issue - not showing validation for Invalid Email Address

I can't see anywhere in your code where you did any validation. If you want to do validation then use corresponding validation attributes on your model property:

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

Obviously the same stands true for your To property if this is intended to be an email:

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

So go get a regex for an email here: http://www.regular-expressions.info/email.html

Upvotes: 2

K D
K D

Reputation: 5989

System.ComponentModel.DataAnnotations.DataType.EmailAddress is not for validating the email. It is used so ASP.Net MVC can decide how to dispaly the value of the field.

You have to write your own logic for email validation. If we apply System.ComponentModel.DataAnnotations.DataType.EmailAddress to any property then that field will be shown with hyperlink and not as a normal text which will have a "mailTo" link added to it. Add your own logic for validating email address by writting custom data annotations.

Cheers

Upvotes: -1

Related Questions