EmailAddress data annotation validation

2 posts, 0 answers
  1. Simon
    Simon avatar
    1 posts
    Member since:
    Jan 2014

    Posted 18 Apr 2018 Link to this post

    Hi there 

    I have a model with a property marked up like this.

    [Display(Name = "Username", ResourceType = typeof(MyResource))]
    public string Username { get; set; }


    The display name of the username property maps to a string "Email".

    When i leave the field in the grid blank and update the row, a validation message is shown that correctly says "Email is required."
    If i provide an invalid email address and update the row, a validation message is shown that incorrectly says "Username is not valid email"

    I have tried applying my own validation message to the Username property with the following modifications to the model.

    [EmailAddress(ErrorMessageResourceName = "InvalidEmail", ErrorMessageResourceType = typeof(MyResource))]
    [Display(Name = "Username", ResourceType = typeof(MyReouce))]
    public string Username {get;set;}


    Both of the above scenarios produce the same results with the above modifications.

    What should the expected behavior be for the email validation message? (e.g use property name or display name attribute)

    How do i provide a custom message for the email validation?

  2. Viktor Tachev
    Viktor Tachev avatar
    2483 posts

    Posted 20 Apr 2018 Link to this post

    Hello Simon,

    By default the EmailAddress attribute will validate it the user input is a valid email. This is why validation fires when only characters are entered. If you would like to customize the message of the error I would suggest defining an ErrorMessage for the validation attribute. Furthermore, check out the stackoverflow thread below that discusses customizing validation messages.

    Viktor Tachev
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top